F# 中有标准选项工作流程吗?
标准 F# 库中是否有(也许)wokflow (monad) 选项?
我发现了十几个手工实现(1, 2)此工作流程,但我真的不想在我的项目中引入非标准且不太可信的代码。所有可以想象到的 google 和 msdn 查询都没有让我知道在哪里可以找到它。
Is there an option (maybe) wokflow (monad) in the standrd F# library?
I've found a dozen of hand-made implementations (1, 2) of this workflow, but I don't really want to introduce non-standard and not very trusted code into my project. And all imaginable queries to google and msdn gave me no clue where to find it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
选项没有标准的计算构建器,但是如果您不需要诸如惰性之类的东西(如您链接的示例中添加的那样),则代码足够简单,没有理由不信任它(特别是考虑到建议命名的
Option .bind
标准库中的函数)。这是一个相当简单的例子:There's no standard computation builder for options, but if you don't need things like laziness (as added in the examples you linked) the code is straightforward enough that there's no reason not to trust it (particularly given the suggestively named
Option.bind
function from the standard library). Here's a fairly minimal example:标准 F# 库中没有 Maybe monad。您可能需要查看 FSharpx,这是由 F# 社区的高素质成员编写的 F# 扩展,其中有相当多有用的 monad。
There's no Maybe monad in the standard F# library. You may want to look at FSharpx, a F# extension written by highly-qualified members of F# community, which has quite a number of useful monads.
我创建了一个可在 nuget 上使用的开源库 FSharp.Interop.NullOptAble 。
它不仅可以用作选项工作流程,还可以用作 null 或可为 null 的工作流程。
效果同样好
或者甚至
如果某些内容为
null
或None
最后,如果您有很多可为 null 类型的内容,我有一个用于
chooseSeq {} 的 cexpr
如果你yield!
一些null
/None
,它就不会被yield。请在此处查看更多示例。
I've create an opensource library FSharp.Interop.NullOptAble available on nuget.
It not only works as an option workflow, but it works as a null or nullable workflow as well.
Works just as well as
Or even
And if something is
null
orNone
Finally if you have a lot of nullable type things, I have a cexpr for
chooseSeq {}
and if youyield!
somethingnull
/None
it just doesn't get yielded.See more examples here.