I have a 'compressed' stream of value to which is attached the number of occurence of that value, for instance :
let a = [ (),1; (),4; (),3;]
I would like to 'uncompress' that sequence and emit the original sequence. I could define a repeat combinator to yield! to that effect
let rec repeat avalue n = seq { if n > 0 then
yield avalue; yield! repeat avalue (n-1) }
let b = seq { for v,n in a do
yield! repeat v n } |> Seq.toList
Is there a way to express that inline, in the form of a composition ?
let c = a |> Seq.XXX(fun e -> ...)
You can do this using Enumerable.Repeat
:
> Seq.collect Enumerable.Repeat [ 1, 2; 3, 4; 5, 6 ] |> List.ofSeq;;
val it : int list = [1; 1; 3; 3; 3; 3; 5; 5; 5; 5; 5; 5]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With