how to assert a tuple in conditional
f#
Solution
The `fst` function can help you out here.
Return the first element of a tuple
An example:
let tuple = (true, 1)
if fst tuple then
//whatever
There's also an `snd` for the second element.
Another alternative is to use pattern matching:
let tuple = (true, 1)
let value =
match tuple with
| (true, _) -> "fst is True"
| (false, _) -> "fst is False"
printfn "%s" value
This can let you match on more complex scenarios, a very powerful construct in F#. Take a look at the Tuple Pattern in the MSDN Documentation for a few examples.
Problem
Given the tuple: ``` let tuple = (true, 1) ``` How do i use this tuple in a conditional? Something like this: ``` if tuple.first then //doesnt work ``` or ``` if x,_ = tuple then // doesnt work ``` I don't want to do this: ``` let isTrue value = let b,_ = value b if isTrue tuple then // boring ``` Is there a nice way to evaluate a tuple value inside a conditional without creating a seperate function ?