when package:ghc-internal

Conditional execution of Applicative expressions. For example,

Examples

when debug (putStrLn "Debugging")
will output the string Debugging if the Boolean value debug is True, and otherwise do nothing.
>>> putStr "pi:" >> when False (print 3.14159)
pi:
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).

Examples

>>> map (\x -> applyWhen (odd x) (*2) x) [1..10]
[2,2,6,4,10,6,14,8,18,10]
>>> map (\x -> applyWhen (length x > 6) ((++ "...") . take 3) x) ["Hi!", "This is amazing", "Hope you're doing well today!", ":D"]
["Hi!","Thi...","Hop...",":D"]

Algebraic properties