Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
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.
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
Check whether all elements are True
.
>>>
run (repeat False) and
False
>>>
run (repeat True) and
... diverges ...
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
Grab the first inputted element.
>>>
run [1 ..] head
Just 1
>>>
run [] head
Nothing
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