FirstOrDefault In F#

前端 未结 4 1070
孤街浪徒
孤街浪徒 2021-01-17 12:27

How to write FirstOrDefault Linq Query in F#? Can I use linq to sql in F# in totally?

4条回答
  •  一生所求
    2021-01-17 12:57

    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
    

提交回复
热议问题