Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# computation expression for nested Boolean tests?

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.

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.

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.

like image 656
MiloDC Avatar asked Dec 07 '22 10:12

MiloDC


1 Answers

You could pull this off without defining a new monad. Just define

let test b = if b then Some () else None

which you can now use with maybe:

maybe {
    do! test (1 > 0)
    printfn "1"
    do! test (2 > 3)
    printfn "2"

    return ()
} 
like image 53
eirik Avatar answered Dec 25 '22 06:12

eirik