when package:relude

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:
Monadic version of when. Conditionally executes the provided action.
>>> whenM (pure False) $ putTextLn "No text :("

>>> whenM (pure True)  $ putTextLn "Yes text :)"
Yes text :)

>>> whenM (Just True) (pure ())
Just ()

>>> whenM (Just False) (pure ())
Just ()

>>> whenM Nothing (pure ())
Nothing
Performs given action over NonEmpty list if given list is non empty.
>>> whenNotNull [] $ \(b :| _) -> print (not b)

>>> whenNotNull [False,True] $ \(b :| _) -> print (not b)
True
Monadic version of whenNotNull.
Applies given action to Either content if Left is given and returns the result. In case of Right the default value will be returned.
>>> whenLeft "bar" (Left 42) (\a -> "success!" <$ print a)
42
"success!"
>>> whenLeft "bar" (Right 42) (\a -> "success!" <$ print a)
"bar"
Monadic version of whenLeft.
>>> whenLeftM "bar" (pure $ Left 42) (\a -> "success!" <$ print a)
42
"success!"
>>> whenLeftM "bar" (pure $ Right 42) (\a -> "success!" <$ print a)
"bar"
Monadic version of whenLeft_.
>>> whenLeftM_ (pure $ Right 42) putTextLn

>>> whenLeftM_ (pure $ Left "foo") putTextLn
foo
Applies given action to Either content if Left is given.
>>> whenLeft_ (Right 42) putTextLn

>>> whenLeft_ (Left "foo") putTextLn
foo
Applies given action to Either content if Right is given and returns the result. In case of Left the default value will be returned.
>>> whenRight "bar" (Left "foo") (\a -> "success!" <$ print a)
"bar"
>>> whenRight "bar" (Right 42) (\a -> "success!" <$ print a)
42
"success!"
Monadic version of whenRight.
>>> whenRightM "bar" (pure $ Left "foo") (\a -> "success!" <$ print a)
"bar"
>>> whenRightM "bar" (pure $ Right 42) (\a -> "success!" <$ print a)
42
"success!"
Monadic version of whenRight_.
>>> whenRightM_ (pure $ Left "foo") print

>>> whenRightM_ (pure $ Right 42) print
42
Applies given action to Either content if Right is given.
>>> whenRight_ (Left "foo") print

>>> whenRight_ (Right 42) print
42
Specialized version of for_ for Maybe. It's used for code readability. Also helps to avoid space leaks: Foldable.mapM_ space leak.
>>> whenJust Nothing $ \b -> print (not b)

>>> whenJust (Just True) $ \b -> print (not b)
False
Monadic version of whenJust.
>>> whenJustM (pure Nothing) $ \b -> print (not b)

>>> whenJustM (pure $ Just True) $ \b -> print (not b)
False
Performs default Applicative action if Nothing is given. Otherwise returns content of Just pured to Applicative.
>>> whenNothing Nothing [True, False]
[True,False]

>>> whenNothing (Just True) [True, False]
[True]
Monadic version of whenNothing.
>>> whenNothingM (pure $ Just True) $ True <$ putTextLn "Is Just!"
True

>>> whenNothingM (pure Nothing) $ False <$ putTextLn "Is Nothing!"
Is Nothing!
False
Monadic version of whenNothing_.
>>> whenNothingM_ (pure $ Just True) $ putTextLn "Is Just!"

>>> whenNothingM_ (pure Nothing) $ putTextLn "Is Nothing!"
Is Nothing!
Performs default Applicative action if Nothing is given. Do nothing for Just. Convenient for discarding Just content.
>>> whenNothing_ Nothing $ putTextLn "Nothing!"
Nothing!

>>> whenNothing_ (Just True) $ putTextLn "Nothing!"