【问题标题】:Making computation expressions using single case discriminated unions使用单例区分联合制作计算表达式
【发布时间】:2017-04-21 07:13:57
【问题描述】:

如果我有一个尝试将起始数字除以两次的函数。整个工作流程必须返回一个布尔值。

let divideBy bottom top =
    if bottom = 0 then None
    else Some (top/bottom)

let divideByWorkflow init x y = 
    match init |> divideBy x with
    | None -> false
    | Some a -> 
        match a |> divideBy y with
        | None -> false
        | Some b -> true

let good = divideByWorkflow 12 3 2
let bad = divideByWorkflow 12 3 0

以下生成器是否正确?

type BoolMaybe = BoolMaybe with
    member __.Bind (expr, f) =
        match expr with
        | Some value -> f value
        | None -> false
    member __.Return expr = expr

let divideByWorkflow init x y =
    BoolMaybe {
        let! a = init |> divideBy x
        let! b = a |> divideBy y
        return true
    }

【问题讨论】:

    标签: .net f# pattern-matching monads computation-expression


    【解决方案1】:

    我同意 Dzoukr 的回答,我的看起来只是略有不同:

    let divideBy bot top =
        match bot with
        | 0 -> None
        | _ -> Some (top / bot)
    
    let divideByWorkflow init x y =
        Some init
        |> Option.bind (divBy x)
        |> Option.bind (divBy y)
        |> Option.isSome
    

    不需要计算表达式,这里似乎没有增加任何好处。

    【讨论】:

    • 不错!这个比我的更优雅。像! ;)
    【解决方案2】:

    恕我直言,没有理由使用计算表达式使其更复杂。我宁愿保留一些简单的功能,例如:

    let divideBy bottom top =
        if bottom = 0 then None
        else Some (top/bottom)
    
    let divideByTwoTimes init x y = 
        init |> divideBy x |> Option.bind (divideBy y)
    
    let divideByWorkflow init x = divideByTwoTimes init x >> Option.isSome
    

    【讨论】:

      【解决方案3】:

      它有效,但我认为它是不必要的具体。如果你确实想使用 CE,我宁愿定义一个普通的Maybe CE 然后做

      let divideByWorkflow init x y =
          Maybe {
              let! a = init |> divideBy x
              let! b = a |> divideBy y
              return true
          }
          |> Option.defaultValue false
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-13
        • 1970-01-01
        相关资源
        最近更新 更多