precursor-0.1.0.0: Prelude replacement

Precursor.Control.Alternative

Synopsis

# Alternative class

class Applicative f => Alternative f where #

A monoid on applicative functors.

If defined, some and many should be the least solutions of the equations:

• some v = (:) <\$> v <*> many v
• many v = some v <|> pure []

Minimal complete definition

Methods

empty :: f a #

The identity of <|>

(<|>) :: f a -> f a -> f a infixl 3 #

An associative binary operation

some :: f a -> f [a] #

One or more.

many :: f a -> f [a] #

Zero or more.

Instances

(<|>) :: Alternative f => forall a. f a -> f a -> f a #

An associative binary operation

empty :: Alternative f => forall a. f a #

The identity of <|>

some :: Alternative f => forall a. f a -> f [a] #

One or more.

many :: Alternative f => forall a. f a -> f [a] #

Zero or more.

# Utility functions

optional :: Alternative f => f a -> f (Maybe a) #

One or none.

afilter :: (Monad m, Alternative m) => (a -> Bool) -> m a -> m a Source #

A generalized version of filter, which works on anything which is both a Monad and Alternative.

\(Blind p) xs -> filter p xs === afilter p xs

asum :: (Foldable t, Alternative f) => t (f a) -> f a #

The sum of a collection of actions, generalizing concat.

guard :: Alternative f => Bool -> f () #

guard b is pure () if b is True, and empty if b is False.

ensure :: Alternative f => (a -> Bool) -> a -> f a Source #

ensure allows you to attach a condition to something

eitherA :: Alternative f => f a -> f b -> f (Either a b) Source #

eitherA is especially useful for parsers.

toAlt :: (Alternative f, Foldable t) => t a -> f a Source #

Convert any Foldable to an Alternative

mapAlt :: (Monad m, Alternative m, Foldable f) => (a -> f b) -> m a -> m b Source #

Map a function over a monad, and concat the results. This is a generalized form of the function mapMaybe.