嵌套布尔测试的F#计算表达式? [英] F# computation expression for nested Boolean tests?

查看:85
本文介绍了嵌套布尔测试的F#计算表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我对F#monad(工作流)有足够的了解,我在代码中看到了一些可以实现它们的地方.

I think I've got enough understanding of F# monads (workflows) that I see a few places in my code where implementing them makes sense.

例如,我有一个带有多个嵌套if/thens的函数,即该函数仅在数据沿途通过某些测试"的情况下才继续.

For example, I've got a function with multiple nested if/thens, i.e. the function should continue only so long as the data pass certain "tests" along the way.

我对也许"单子很熟悉,但是在我看到的所有示例中,它都被编码为对let!绑定进行操作,而我并没有这样做.我希望有人可以为我提供一个为嵌套布尔测试而不是let绑定量身定制的也许"工作流的示例.

I'm familiar with the "maybe" monad, but in all the examples that I've seen, it's coded to operate on let! bindings, which I'm not doing. I'm hoping that someone can provide me with an example of the "maybe" workflow tailored for nested Boolean tests, not let binds.

推荐答案

我提供了一个有条件的工作流来响应类似的问题.我将其复制到此处,以供参考.

I offered up a conditional workflow in response to a similar problem. I'll copy it here, for reference.

module Condition =
  type ConditionBuilder() =
    member x.Bind(v, f) = if v then f() else false
    member x.Return(v) = v
  let condition = ConditionBuilder()

open Condition

let eval() =
  condition {
    // do some work
    do! conditionA
    // do some work
    do! conditionB
    // do some work
    do! conditionC
    return true
  }

您可以在对我的上一个答案的评论中看到,并非所有人都是粉丝.但这仍然很有趣.

As you can see in the comments to my previous answer, not everyone's a fan. But it's interesting nonetheless.

这篇关于嵌套布尔测试的F#计算表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆