Re:Functional isn't always better
by Strilanc on Friday October 07, 2011 @12:15PM
(#3084622)
Adding to the list of simple implementations, here's one in C#:
I think the author started with the wrong idea and didn't backtrack far enough before giving up.
[ Parent
| on HN ]
The only problem here is that each time it has to skip through the entire list so far. I would write it like this in F# for equivalent generality and lazinessas yours
let pairs sequence =
sequence |> Seq.fold (fun (tailList, listOfPairs) el ->
let listThusFar = tailList |> Seq.map (fun x -> el, x)
(tailList |> Seq.skip 1), listThusFar::listOfPairs)
((sequence |> Seq.skip 1),[])
|> snd |> Seq.concat