4 ms·
Adding to the list of simple implementations, here's one in C#: list.SelectMany((e, i) => list.Skip(i+1).Select(f => Tuple.Create(e, f))) I think the auth
by Strilanc 15y ago
Adding to the list of simple implementations, here's one in C#:
list.SelectMany((e, i) => list.Skip(i+1).Select(f => Tuple.Create(e, f)))
I think the author started with the wrong idea and didn't backtrack far enough before giving up.
- Dn_Ab 15y agoThe 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