F# 中有标准选项工作流程吗?

发布于 2024-12-11 00:50:06 字数 400 浏览 0 评论 0原文

标准 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我不咬妳我踢妳 2024-12-18 00:50:07

选项没有标准的计算构建器,但是如果您不需要诸如惰性之类的东西(如您链接的示例中添加的那样),则代码足够简单,没有理由不信任它(特别是考虑到建议命名的 Option .bind 标准库中的函数)。这是一个相当简单的例子:

type OptionBuilder() =
    member x.Bind(v,f) = Option.bind f v
    member x.Return v = Some v
    member x.ReturnFrom o = o
    member x.Zero () = None

let opt = OptionBuilder()

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:

type OptionBuilder() =
    member x.Bind(v,f) = Option.bind f v
    member x.Return v = Some v
    member x.ReturnFrom o = o
    member x.Zero () = None

let opt = OptionBuilder()
傲鸠 2024-12-18 00:50:07

标准 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.

橘亓 2024-12-18 00:50:07

我创建了一个可在 nuget 上使用的开源库 FSharp.Interop.NullOptAble

它不仅可以用作选项工作流程,还可以用作 null 或可为 null 的工作流程。

let x = Nullable(3)
let y = Nullable(3)
option {
    let! x' = x
    let! y' = y
    return (x' + y')
} (* |> should equal (Some 6) *)

效果同样好

let x = Some(3)
let y = Some(3)
option {
    let! x' = x
    let! y' = y
    return (x' + y')
} (* |> should equal (Some 6) *)

或者甚至

let x = "Hello "
let y = "World"
option {
    let! x' = x
    let! y' = y
    return (x' + y')
} (* |> should equal (Some "Hello World") *)

如果某些内容为 nullNone

let x = "Hello "
let y:string = null
option {
    let! x' = x
    let! y' = y
    return (x' + y')
} (* |> should equal 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.

let x = Nullable(3)
let y = Nullable(3)
option {
    let! x' = x
    let! y' = y
    return (x' + y')
} (* |> should equal (Some 6) *)

Works just as well as

let x = Some(3)
let y = Some(3)
option {
    let! x' = x
    let! y' = y
    return (x' + y')
} (* |> should equal (Some 6) *)

Or even

let x = "Hello "
let y = "World"
option {
    let! x' = x
    let! y' = y
    return (x' + y')
} (* |> should equal (Some "Hello World") *)

And if something is null or None

let x = "Hello "
let y:string = null
option {
    let! x' = x
    let! y' = y
    return (x' + y')
} (* |> should equal None *)

Finally if you have a lot of nullable type things, I have a cexpr for chooseSeq {} and if you yield! something null/None it just doesn't get yielded.

See more examples here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文