【问题标题】:Cannot define a computation expression custom "condition" operator无法定义计算表达式自定义“条件”运算符
【发布时间】:2017-08-23 19:41:39
【问题描述】:

我很难在以下代码中为condition 定义一个定义。希望有一个例子和见解:

// a computation expression builder class
type Builder() =
    .
    .
    .

    [<CustomOperation( "condition",
      MaintainsVariableSpaceUsingBind = true )>]
    member this.Condition(p, [<ProjectionParameter>] b) = 
        condition p b

let attemp = AttemptBuilder()

let test =
    attempt { let x, y = exp1, exp2
              condition booleanExpr(x, y)   
              return (x, y) }

我认为b 隐含地是( fun x, y -&gt; booleanExpr(x, y) )。术语booleanExpr(x, y) 只是一些涉及xy 的布尔表达式。

【问题讨论】:

标签: f# computation-expression custom-operator


【解决方案1】:

找到了:

let condition p guard = ( fun () ->
    match p() with
    | Some x when guard x -> Some x
    | _ -> None )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    相关资源
    最近更新 更多