Search code examples
f#tuplesinlineiterable-unpacking

Squashing tuples from (a,(b,c)) to (a,b,c) in fsharp


Does it make sense to have such functions defined

let squash12 (e:('a*('b*'c)   )) = e |> (fun (a,(b,c)  ) -> (a,b,c  ))
let squash21 (e:(('a*'b)*'c   )) = e |> (fun ((a,b),c  ) -> (a,b,c  ))
let squash13 (e:('a*('b*'c*'d))) = e |> (fun (a,(b,c,d)) -> (a,b,c,d))

let seqsquash12 (sa:seq<'T>) = sa |> Seq.map squash12
let seqsquash21 (sa:seq<'T>) = sa |> Seq.map squash21
let seqsquash13 (sa:seq<'T>) = sa |> Seq.map squash13

I could not find another way to make my core code recursive (leading to nested tuples), yet expose simple function that maps to generalized n-dimensional coordinates.


Solution

  • I would have marked your functions as inline so that they could just be

    let inline squash1 (a,(b,c)) = (a,b,c)
    

    Also, you don't need the lambdas (fun ...)