9 people like it.

EnumerableStream

A lazy enumerable/stream of bytes.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
open System
open System.Collections
open System.Collections.Generic
open System.Diagnostics.Contracts
open System.IO

type EnumerableStream(data:seq<byte>) =
  inherit Stream()
  do Contract.Requires(data <> null)
  let d = data.GetEnumerator()

  interface IEnumerable<byte> with
    member this.GetEnumerator() = d
    member this.GetEnumerator() = d :> IEnumerator 

  override this.CanRead = true
  override this.CanSeek = true
  override this.CanWrite = false
  override this.Flush() = ()
  override this.Length = data |> Seq.length |> int64
  override this.Position with get() = raise (NotSupportedException())
                         and set(v) = raise (NotSupportedException())
  override this.Seek(offset, origin) = raise (NotSupportedException())
  override this.SetLength(value) = raise (NotSupportedException())
  override this.Write(buffer, offset, count) = raise (NotSupportedException())
  override this.Dispose(disposing) = d.Dispose()
                                     base.Dispose(disposing)
  override this.Read(buffer, offset, count) =
    Contract.Requires(buffer <> null)
    Contract.Requires(offset >= 0)
    Contract.Requires(count > 0)
    Contract.Requires(offset + count <= buffer.Length)

    let rec loop bytesRead =
      if d.MoveNext() && bytesRead < count
        then
          buffer.[bytesRead + offset] <- d.Current
          loop (bytesRead + 1)
        else bytesRead
    loop 0
namespace System
namespace System.Collections
namespace System.Collections.Generic
namespace System.Diagnostics
namespace System.Diagnostics.Contracts
namespace System.IO
Multiple items
type EnumerableStream =
  inherit Stream
  interface IEnumerable<byte>
  new : data:seq<byte> -> EnumerableStream
  override Dispose : disposing:bool -> unit
  override Flush : unit -> unit
  override Read : buffer:byte [] * offset:int * count:int -> int
  override Seek : offset:int64 * origin:SeekOrigin -> int64
  override SetLength : value:int64 -> unit
  override Write : buffer:byte [] * offset:int * count:int -> unit
  override CanRead : bool
  ...

Full name: Script.EnumerableStream

--------------------
new : data:seq<byte> -> EnumerableStream
val data : seq<byte>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
Multiple items
val byte : value:'T -> byte (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.byte

--------------------
type byte = Byte

Full name: Microsoft.FSharp.Core.byte
Multiple items
type Stream =
  inherit MarshalByRefObject
  member BeginRead : buffer:byte[] * offset:int * count:int * callback:AsyncCallback * state:obj -> IAsyncResult
  member BeginWrite : buffer:byte[] * offset:int * count:int * callback:AsyncCallback * state:obj -> IAsyncResult
  member CanRead : bool
  member CanSeek : bool
  member CanTimeout : bool
  member CanWrite : bool
  member Close : unit -> unit
  member CopyTo : destination:Stream -> unit + 1 overload
  member Dispose : unit -> unit
  member EndRead : asyncResult:IAsyncResult -> int
  ...

Full name: System.IO.Stream

--------------------
Stream() : unit
type Contract =
  static member Assert : condition:bool -> unit + 1 overload
  static member Assume : condition:bool -> unit + 1 overload
  static member EndContractBlock : unit -> unit
  static member Ensures : condition:bool -> unit + 1 overload
  static member EnsuresOnThrow<'TException> : condition:bool -> unit + 1 overload
  static member Exists<'T> : collection:IEnumerable<'T> * predicate:Predicate<'T> -> bool + 1 overload
  static member ForAll<'T> : collection:IEnumerable<'T> * predicate:Predicate<'T> -> bool + 1 overload
  static member Invariant : condition:bool -> unit + 1 overload
  static member OldValue<'T> : value:'T -> 'T
  static member Requires : condition:bool -> unit + 3 overloads
  ...

Full name: System.Diagnostics.Contracts.Contract
Contract.Requires<'TException (requires 'TException :> exn)>(condition: bool) : unit
Contract.Requires(condition: bool) : unit
Contract.Requires<'TException (requires 'TException :> exn)>(condition: bool, userMessage: string) : unit
Contract.Requires(condition: bool, userMessage: string) : unit
val d : IEnumerator<byte>
IEnumerable.GetEnumerator() : IEnumerator<byte>
Multiple items
type IEnumerable =
  member GetEnumerator : unit -> IEnumerator

Full name: System.Collections.IEnumerable

--------------------
type IEnumerable<'T> =
  member GetEnumerator : unit -> IEnumerator<'T>

Full name: System.Collections.Generic.IEnumerable<_>
val this : EnumerableStream
override EnumerableStream.GetEnumerator : unit -> IEnumerator<byte>

Full name: Script.EnumerableStream.GetEnumerator
override EnumerableStream.GetEnumerator : unit -> IEnumerator

Full name: Script.EnumerableStream.GetEnumerator
Multiple items
type IEnumerator =
  member Current : obj
  member MoveNext : unit -> bool
  member Reset : unit -> unit

Full name: System.Collections.IEnumerator

--------------------
type IEnumerator<'T> =
  member Current : 'T

Full name: System.Collections.Generic.IEnumerator<_>
override EnumerableStream.CanRead : bool

Full name: Script.EnumerableStream.CanRead
override EnumerableStream.CanSeek : bool

Full name: Script.EnumerableStream.CanSeek
override EnumerableStream.CanWrite : bool

Full name: Script.EnumerableStream.CanWrite
override EnumerableStream.Flush : unit -> unit

Full name: Script.EnumerableStream.Flush
override EnumerableStream.Length : int64

Full name: Script.EnumerableStream.Length
module Seq

from Microsoft.FSharp.Collections
val length : source:seq<'T> -> int

Full name: Microsoft.FSharp.Collections.Seq.length
Multiple items
val int64 : value:'T -> int64 (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int64

--------------------
type int64 = Int64

Full name: Microsoft.FSharp.Core.int64

--------------------
type int64<'Measure> = int64

Full name: Microsoft.FSharp.Core.int64<_>
override EnumerableStream.Position : int64 with set

Full name: Script.EnumerableStream.Position
val raise : exn:Exception -> 'T

Full name: Microsoft.FSharp.Core.Operators.raise
Multiple items
type NotSupportedException =
  inherit SystemException
  new : unit -> NotSupportedException + 2 overloads

Full name: System.NotSupportedException

--------------------
NotSupportedException() : unit
NotSupportedException(message: string) : unit
NotSupportedException(message: string, innerException: exn) : unit
val set : elements:seq<'T> -> Set<'T> (requires comparison)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.set
val v : int64
override EnumerableStream.Seek : offset:int64 * origin:SeekOrigin -> int64

Full name: Script.EnumerableStream.Seek
val offset : int64
val origin : SeekOrigin
override EnumerableStream.SetLength : value:int64 -> unit

Full name: Script.EnumerableStream.SetLength
val value : int64
override EnumerableStream.Write : buffer:byte [] * offset:int * count:int -> unit

Full name: Script.EnumerableStream.Write
val buffer : byte []
val offset : int
val count : int
override EnumerableStream.Dispose : disposing:bool -> unit

Full name: Script.EnumerableStream.Dispose
val disposing : bool
IDisposable.Dispose() : unit
override EnumerableStream.Read : buffer:byte [] * offset:int * count:int -> int

Full name: Script.EnumerableStream.Read
property Array.Length: int
val loop : (int -> int)
val bytesRead : int
IEnumerator.MoveNext() : bool
property IEnumerator.Current: byte
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/1s
Posted:14 years ago
Author:Ryan Riley
Tags: sequence , stream