我今天尝试使用 Seq.first,编译器说它已被弃用,取而代之的是 Seq.tryPick。它说它应用了一个函数并返回返回 Some 的第一个结果。我想我只能说 fun x -> x!=0 因为我知道第一个在我的情况下会返回 Some ,但是放在这里的正确约束是什么?什么是正确的语法?
为了澄清,我想以以下格式使用它:
let foo(x:seq<int>) =
x.filter(fun x -> x>0)
|> Seq.tryPick (??)
关键是 'Seq.first' 没有返回第一个元素,而是返回了与某些 'choose' 谓词匹配的第一个元素:
let a = [1;2;3]
// two ways to select the first even number (old name, new name)
let r1 = a |> Seq.first (fun x -> if x%2=0 then Some(x) else None)
let r2 = a |> Seq.tryPick (fun x -> if x%2=0 then Some(x) else None)
如果您只想要第一个元素,请使用 Seq.head
let r3 = a |> Seq.head