Fst and 3-tuple in fsharp

Do you know the nicest way to do this job:

let toTableau2D (seqinit:seq<'a*'b*'c>) = let myfst = fun (a,b,c) -> a let myscd = fun (a,b,c) -> b let mytrd = fun (a,b,c) -> c let inputd = seqinit |> groupBy2 myfst myscd 

there should be a better way than rewriting fst ..

UPDATE After consulting with the pad, I rewrote the packaging of the previous 'a *' b into a single structure. Now my code looks like

 let toTableau (seqinit:seq<'a*'b>) = let inputd = seqinit |> Seq.groupBy fst |> toMap let keys = seqinit |> Seq.map fst |> Set.ofSeq |> List.ofSeq ... 
+4
source share
2 answers

Why don't you just write it explicitly:

 let toTableau2D (a, b, c) = let toto = a // ... 

If you want to refer to seqinit , you can always restore the triple or use a named template:

 let toTableau2D ((a, b, c) as seqinit) = let toto = a // Do something with seqinit // ... 

EDIT:

If you are not using reflection, you cannot have the fst function for any type of tuple. In your example, writing some utility functions and reusing them will not hurt:

 let fst3 (a, _, _) = a let snd3 (_, b, _) = b let thd3 (_, _, c) = c let toTableau2D (seqinit: seq<'a*'b*'c>) = let inputd = seqinit |> groupBy2 fst3 snd3 // ... 

If you want to make this work for an arbitrary number of tuple elements, consider changing tuples in lists and using pattern matching in lists.

+5
source

+1 what @pad said. Otherwise (if you just simplified what you are trying to do and are stuck with seqinit defined this way), I think you can always:

 let toTableau2D (seqinit:'a*'b*'c) = let toto, _, _ = seqinit //... 
+2
source

Source: https://habr.com/ru/post/1399202/


All Articles