5 people like it.
Like the snippet!
Convert list of sequences to sequence of lists
Function to combine the values of a list of sequences into a sequence of lists where each list contains the nth element of the different sequences. It works like zip but for an arbitrary number of sequences and it returns lists instead of tuples. As with zip when one sequence is exhausted any remaining elements in the other sequences are ignored.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
|
// converts a list of sequences to a sequence of lists
// when one sequence is exhausted any remaining elements in the other sequences are ignored
let zipseq (sequencelist:list<seq<'a>>) =
let enumerators = sequencelist |> List.map (fun (s:seq<'a>) -> (s.GetEnumerator()))
seq {
let hasNext() = enumerators |> List.exists (fun e -> not (e.MoveNext())) |> not
while hasNext() do
yield enumerators |> List.map (fun e -> e.Current)
}
let sample() =
let one23 = zipseq [seq {1 .. 3}; seq {1 .. 4}; seq{1 .. 3}]
printfn "%A" one23 // prints: seq [[1; 1; 1]; [2; 2; 2]; [3; 3; 3]]
|
val zipseq : sequencelist:seq<'a> list -> seq<'a list>
Full name: Script.zipseq
val sequencelist : seq<'a> list
type 'T list = List<'T>
Full name: Microsoft.FSharp.Collections.list<_>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>
Full name: Microsoft.FSharp.Core.Operators.seq
--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>
Full name: Microsoft.FSharp.Collections.seq<_>
val enumerators : System.Collections.Generic.IEnumerator<'a> list
Multiple items
module List
from Microsoft.FSharp.Collections
--------------------
type List<'T> =
| ( [] )
| ( :: ) of Head: 'T * Tail: 'T list
interface IEnumerable
interface IEnumerable<'T>
member Head : 'T
member IsEmpty : bool
member Item : index:int -> 'T with get
member Length : int
member Tail : 'T list
static member Cons : head:'T * tail:'T list -> 'T list
static member Empty : 'T list
Full name: Microsoft.FSharp.Collections.List<_>
val map : mapping:('T -> 'U) -> list:'T list -> 'U list
Full name: Microsoft.FSharp.Collections.List.map
val s : seq<'a>
System.Collections.Generic.IEnumerable.GetEnumerator() : System.Collections.Generic.IEnumerator<'a>
val hasNext : (unit -> bool)
val exists : predicate:('T -> bool) -> list:'T list -> bool
Full name: Microsoft.FSharp.Collections.List.exists
val e : System.Collections.Generic.IEnumerator<'a>
val not : value:bool -> bool
Full name: Microsoft.FSharp.Core.Operators.not
System.Collections.IEnumerator.MoveNext() : bool
property System.Collections.Generic.IEnumerator.Current: 'a
val sample : unit -> unit
Full name: Script.sample
val one23 : seq<int list>
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
More information