folds-common-0.2.0.0: A playground of common folds for folds

Safe HaskellSafe
LanguageHaskell2010

Data.Fold.Common.M

Description

A collection of monoidal folds. These are all short circuiting and are designed to handle certain infinite cases properly. These are useful for operations which don't require the full list to calculate the output.

Synopsis

Documentation

any :: (a -> Bool) -> M a Bool Source

Check that if predicate holds for any inputs to the fold.

>>> run [1, 2, 3, 4] (any even)
True
>>> run [] (any $ const False)
False

all :: (a -> Bool) -> M a Bool Source

Check that if predicate holds for all inputs to the fold.

>>> run [1, 2, 3, 4] (all (< 6))
True
>>> run [1, 2, 3, 4] (all (> 1))
False

and :: M Bool Bool Source

Check whether all elements are True.

>>> run (repeat False) and
False
>>> run (repeat True) and
... diverges ...

or :: M Bool Bool Source

Check whether any elements are True.

>>> run (True : repeat False) or
True
>>> run (repeat False) or
... diverges ...

elem :: Eq a => a -> M a Bool Source

Check whether an element is fed into the fold.

>>> run [1, 2, 3] (elem 3)
True
>>> run [] (elem 1)
False

notElem :: Eq a => a -> M a Bool Source

Check whther an element isn't fed into the fold. >>> run [1, 2, 3] (notElem 3) False

>>> run [] (notElem 1)
True

find :: (a -> Bool) -> M a (Maybe a) Source

Find the first element for which a predicate holds.

>>> run [1, 2, 3, 4] (find even)
Just 2
>>> run [1, 2, 3, 4] (find (> 4))
Nothing

head :: M a (Maybe a) Source

Grab the first inputted element.

>>> run [1 ..] head
Just 1
>>> run [] head
Nothing

null :: M a Bool Source

Check whether a fold was fed any elements.

>>> run [] null
True
>>> run [1..] null
False

strictify :: M a b -> L' a b Source

Occasionally we want to use a short-circuiting fold with other, nonlazy folds. This function drops laziness on the floor for a L' fold. This is dangerous because it can potentially effect termination behavior.

>>> run (repeat False) and
False
>>> run (repeat False) (strictify and)
... diverges ...

This is generally an advantage when we want to combine a monoidal fold with a left one.

>>> run [1.0, 2, 3, 4] $ (/) <$> strictify head <*> maximum
0.25