I want to create a function with a signature seq<#seq<'a>> ->seq<seq<'a>>that acts like a Zip method, taking a sequence of an arbitrary number of input sequences (instead of 2 or 3, as in Zip2 and Zip3) and instead returns a sequence of sequences of tuples as a result.
That is, given the following input:
[[1;2;3];
[4;5;6];
[7;8;9]]
it will return the result: [[1; 4; 7]; [2; 5; 8]; [3; 6; 9]]
excluding sequences instead of lists.
I am very new to F #, but I created a function that does what I want, but I know that it can be improved. This is not tail recursive, and it seems like it could be simpler, but I don't know yet. I also did not find a good way to get the signature the way I want (taking, for example, int list listas input) without a second function.
I know that this can be implemented using counters directly, but I'm interested in doing this functionally.
Here is my code:
let private Tail seq = Seq.skip 1 seq
let private HasLengthNoMoreThan n = Seq.skip n >> Seq.isEmpty
let rec ZipN_core = function
| seqs when seqs |> Seq.isEmpty -> Seq.empty
| seqs when seqs |> Seq.exists Seq.isEmpty -> Seq.empty
| seqs ->
let head = seqs |> Seq.map Seq.head
let tail = seqs |> Seq.map Tail |> ZipN_core
Seq.append (Seq.singleton head) tail
let ZipN seqs = seqs |> Seq.map (fun x -> x |> Seq.map (fun y -> y)) |> ZipN_core
source
share