如何使用 tryPick 获取序列的第一个元素?
我今天尝试使用 Seq.first,编译器表示它已被弃用,转而使用 Seq.tryPick。它表示它应用一个函数并返回第一个返回 Some 的结果。我想我只能说有趣 x -> x!=0 因为我知道第一个在我的情况下会返回 Some,但是在这里放置的正确约束是什么?正确的语法是什么?
为了澄清,我想以以下格式使用它:
let foo(x:seq<int>) =
x.filter(fun x -> x>0)
|> Seq.tryPick (??)
I was trying to use Seq.first today, and the compiler says it has been deprecated in favor of Seq.tryPick. It says that it applies a function and returns the first result that returns Some. I guess I can just say fun x -> x!=0 since I know the first one will return Some in my case, but what is the proper constraint to put here? What is the correct syntax?
To clarify, I want to use it in the format:
let foo(x:seq<int>) =
x.filter(fun x -> x>0)
|> Seq.tryPick (??)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关键是“Seq.first”没有返回第一个元素,而是返回与某些“choose”谓词匹配的第一个元素:
如果您只想要第一个元素,请使用 Seq.head
The key is that 'Seq.first' did not return the first element, rather it returned the first element that matched some 'choose' predicate:
If you just want the first element, use Seq.head