Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeating sequences in F#

Tags:

f#

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 -> ...) 
like image 520
nicolas Avatar asked Apr 02 '13 17:04

nicolas


1 Answers

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]
like image 132
MisterMetaphor Avatar answered Nov 30 '22 02:11

MisterMetaphor