2
\$\begingroup\$

I have code that zips 3 lists, finds a maximum and extracts part of the max tuple. Isn't there a shorter way to do this in F#?

let Triplets = List.zip3 A B C
let T1 (x, _, _) = x
let T2 (_, x, _) = x
let T3 (_, _, x) = x
let Best = List.maxBy T3 Triplets
T1 Best,T2 Best // return to C# code
svick
24.5k4 gold badges53 silver badges89 bronze badges
asked Mar 1, 2015 at 14:28
\$\endgroup\$
1
  • \$\begingroup\$ You could obviously combine T1 and T2 to T12 (x, y, _) = (x, y). The rest of your solution looks pretty idiomatic to me. However, the need of such "zip3-max-extract" operation chain could be a sign for a design flaw somewhere else in your logic. \$\endgroup\$ Commented Mar 1, 2015 at 15:40

1 Answer 1

3
\$\begingroup\$

One way to simplify this would be to use nested pairs (('a * 'b) * 'c) instead of a triple ('a * 'b * 'c). That way, you can use fst and snd to get to the parts that you want:

let triplets = List.zip (List.zip A B) C
let best = List.maxBy snd triplets
fst best

Notice that I also changed the names of variables to lowercase, that is the common naming convention in F#.

answered Mar 1, 2015 at 16:09
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.