when package:rebase
Conditional execution of
Applicative expressions. For example,
when debug (putStrLn "Debugging")
will output the string
Debugging if the Boolean value
debug is
True, and otherwise do nothing.
The
whenLeft function takes an
Either value and a
function which returns a monad. The monad is only executed when the
given argument takes the form
Left _, otherwise it
does nothing.
Using
Control.Lens:
whenLeft ≡ forOf_ _Left
>>> whenLeft (Left 12) print
12
The
whenRight function takes an
Either value and a
function which returns a monad. The monad is only executed when the
given argument takes the form
Right _, otherwise it
does nothing.
Using
Data.Foldable:
whenRight ≡ forM_
Using
Control.Lens:
whenRight ≡ forOf_ _Right
>>> whenRight (Right 12) print
12
Conditionally perform an effect.
applyWhen applies a function to a value if a condition is true,
otherwise, it returns the value unchanged.
It is equivalent to
flip (bool id).
Algebraic properties: