utility-ht-0.0.17.2: Various small helper functions for Lists, Maybes, Tuples, Functions

Safe Haskell | Safe-Inferred
Language | Haskell98

Data.Maybe.HT

Synopsis

- toMaybe :: Bool -> a -> Maybe a
- (?->) :: Maybe a -> (a -> b) -> Maybe b
- alternatives :: a -> [Maybe a] -> a

# Documentation

    >>> import Control.Monad (guard)

toMaybe :: Bool -> a -> Maybe a Source #

Returns `Just` if the precondition is fulfilled. prop> b x -> (guard b >> x) == (toMaybe b =<< (x::Maybe Char))

(?->) :: Maybe a -> (a -> b) -> Maybe b infixl 6 Source #

This is an infix version of `fmap` for writing `select` style expressions using test functions, that produce `Maybe`s.

The precedence is chosen to be higher than `(:)`, in order to allow:

    alternatives default $
       checkForA ?-> (\a -> f a) :
       checkForB ?-> (\b -> g b) :
       []

The operation is left associative in order to allow to write

    checkForA ?-> f ?-> g

which is equivalent to

    checkForA ?-> g . f

due to the functor law.

alternatives :: a -> [Maybe a] -> a Source #