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

Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.Fold.Common.M

Description

A collection of right 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 ...

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

indexOf :: Enum e => (a -> Bool) -> M a (Maybe e) Source

Find the first index for which a predicate holds.

>>> run [1, 2, 3, 4] (indexOf (== 4))
Just 3
>>> run [1, 2, 3, 4] (indexOf (> 4))
Nothing

head :: M a (Maybe a) Source

Grab the first inputted element.

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

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 means it is only advisable to use when combining a monoidal fold with something that requires left folding.

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