Safe Haskell | Safe-Inferred |
---|
This module provides efficient and streaming left folds that you can combine
using Applicative
style.
Import this module qualified to avoid clashing with the Prelude:
>>>
import qualified Control.Foldl as L
Use fold
to apply a Fold
to a list:
>>>
L.fold L.sum [1..100]
5050
Fold
s are Applicative
s, so you can combine them using Applicative
combinators:
>>>
import Control.Applicative
>>>
let average = (/) <$> L.sum <*> L.genericLength
These combined folds will still traverse the list only once, streaming efficiently over the list in constant space without space leaks:
>>>
L.fold average [1..10000000]
5000000.5>>>
L.fold ((,) <$> L.minimum <*> L.maximum) [1..10000000]
(Just 1,Just 10000000)
You can also unpack the Fold
type if you want to extract the individual
components of combined folds for use with your own customized folding
utilities:
case ((/) <$> L.sum <*> L.genericLength) of L.Foldl step begin done -> ...
- data Fold a b = forall x . Fold (x -> a -> x) x (x -> b)
- fold :: Fold a b -> [a] -> b
- data FoldM m a b = forall x . FoldM (x -> a -> m x) (m x) (x -> m b)
- foldM :: Monad m => FoldM m a b -> [a] -> m b
- mconcat :: Monoid a => Fold a a
- foldMap :: Monoid w => (a -> w) -> (w -> b) -> Fold a b
- head :: Fold a (Maybe a)
- last :: Fold a (Maybe a)
- null :: Fold a Bool
- length :: Fold a Int
- and :: Fold Bool Bool
- or :: Fold Bool Bool
- all :: (a -> Bool) -> Fold a Bool
- any :: (a -> Bool) -> Fold a Bool
- sum :: Num a => Fold a a
- product :: Num a => Fold a a
- maximum :: Ord a => Fold a (Maybe a)
- minimum :: Ord a => Fold a (Maybe a)
- elem :: Eq a => a -> Fold a Bool
- notElem :: Eq a => a -> Fold a Bool
- find :: (a -> Bool) -> Fold a (Maybe a)
- index :: Int -> Fold a (Maybe a)
- elemIndex :: Eq a => a -> Fold a (Maybe Int)
- findIndex :: (a -> Bool) -> Fold a (Maybe Int)
- genericLength :: Num b => Fold a b
- genericIndex :: Integral i => i -> Fold a (Maybe a)
Fold Types
Efficient representation of a left fold that preserves the fold's step function, initial accumulator, and extraction function
This allows the Applicative
instance to assemble derived folds that
traverse the container only once
forall x . Fold (x -> a -> x) x (x -> b) |
Functor (Fold a) | |
Applicative (Fold a) |
Folds
head :: Fold a (Maybe a)Source
Get the first element of a container or return Nothing
if the container is
empty
last :: Fold a (Maybe a)Source
Get the last element of a container or return Nothing
if the container is
empty
find :: (a -> Bool) -> Fold a (Maybe a)Source
(find predicate)
returns the first element that satisfies the predicate or
Nothing
if no element satisfies the predicate
index :: Int -> Fold a (Maybe a)Source
(index n)
returns the n
th element of the container, or Nothing
if the
container has an insufficient number of elements
elemIndex :: Eq a => a -> Fold a (Maybe Int)Source
(elemIndex a)
returns the index of the first element that equals a
, or
Nothing
if no element matches
findIndex :: (a -> Bool) -> Fold a (Maybe Int)Source
(findIndex predicate)
returns the index of the first element that
satisfies the predicate, or Nothing
if no element satisfies the predicate