2

If I have a function that attempts to divide a starting number two times. The entire workflow has to return a boolean.

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

Is the following builder correct?

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
    }
MiP
  • 5,846
  • 3
  • 26
  • 41

3 Answers3

4

I concur with Dzoukr's answer, mine looks just slightly different:

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

No computation expression required, it does not seem to add any benefits here.

dumetrulo
  • 1,993
  • 9
  • 11
3

IMHO there is no reason to make it more complex using computation expression. I would rather stay with few simple functions like:

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
Honza Brestan
  • 10,637
  • 2
  • 32
  • 43
Dzoukr
  • 1,094
  • 1
  • 13
  • 17
2

It works, but I think it's unnecessarily specific. If you do want to use a CE, I would rather define a normal Maybe CE and then do

let divideByWorkflow init x y =
    Maybe {
        let! a = init |> divideBy x
        let! b = a |> divideBy y
        return true
    }
    |> Option.defaultValue false
Tarmil
  • 11,177
  • 30
  • 35