如果我有一个试图将起始值分成两次的功能。整个工作流程必须返回一个布尔值。
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
}
答案 0 :(得分:4)
我同意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
不需要计算表达式,这似乎没有任何好处。
答案 1 :(得分:3)
恕我直言,没有理由使用计算表达式使其更复杂。我宁愿留下一些简单的功能,如:
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
答案 2 :(得分:2)
它有效,但我认为这是不必要的具体。如果你确实想要使用CE,我宁愿定义一个普通的Maybe
CE,然后再做
let divideByWorkflow init x y =
Maybe {
let! a = init |> divideBy x
let! b = a |> divideBy y
return true
}
|> Option.defaultValue false