How to write FirstOrDefault Linq Query in F#? Can I use linq to sql in F# in totally?
Note that a more idiomatic approach within F# would probably be to use something along the lines of Seq.tryFind
rather than to use the LINQ operators, although it's not a drop in replacement since it returns an option value.
Because the Seq
module already has a head
function of type seq<'a> -> 'a
, I would define a function tryHead
with signature seq<'a> -> option<'a>
:
module Seq =
let tryHead (ls:seq<'a>) : option<'a> = ls |> Seq.tryPick Some
using it as:
[1; 2; 3] |> Seq.tryHead
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With