Hoogle Search

Within LTS Haskell 24.4 (ghc-9.10.2)

Note that Stackage only displays results for the latest LTS and Nightly snapshot. Learn more.

  1. foldl1' :: HasCallStack => (a -> a -> a) -> [a] -> a

    base Data.List

    A strict version of foldl1.

  2. foldl' :: Foldable t => (b -> a -> b) -> b -> t a -> b

    base Data.Foldable

    Left-associative fold of a structure but with strict application of the operator. This ensures that each step of the fold is forced to Weak Head Normal Form before being applied, avoiding the collection of thunks that would otherwise occur. This is often what you want to strictly reduce a finite structure to a single strict result (e.g. sum). For a general Foldable structure this should be semantically identical to,

    foldl' f z = foldl' f z . toList
    

  3. foldl1 :: Foldable t => (a -> a -> a) -> t a -> a

    base Data.Foldable

    A variant of foldl that has no base case, and thus may only be applied to non-empty structures. This function is non-total and will raise a runtime exception if the structure happens to be empty.

    foldl1 f = foldl1 f . toList
    

    Examples

    Basic usage:
    >>> foldl1 (+) [1..4]
    10
    
    >>> foldl1 (+) []
    *** Exception: Prelude.foldl1: empty list
    
    >>> foldl1 (+) Nothing
    *** Exception: foldl1: empty structure
    
    >>> foldl1 (-) [1..4]
    -8
    
    >>> foldl1 (&&) [True, False, True, True]
    False
    
    >>> foldl1 (||) [False, False, True, True]
    True
    
    >>> foldl1 (+) [1..]
    * Hangs forever *
    

  4. foldlM :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m b

    base Data.Foldable

    Left-to-right monadic fold over the elements of a structure. Given a structure t with elements (a, b, ..., w, x, y), the result of a fold with an operator function f is equivalent to:

    foldlM f z t = do
    aa <- f z a
    bb <- f aa b
    ...
    xx <- f ww x
    yy <- f xx y
    return yy -- Just @return z@ when the structure is empty
    
    For a Monad m, given two functions f1 :: a -> m b and f2 :: b -> m c, their Kleisli composition (f1 >=> f2) :: a -> m c is defined by:
    (f1 >=> f2) a = f1 a >>= f2
    
    Another way of thinking about foldlM is that it amounts to an application to z of a Kleisli composition:
    foldlM f z t =
    flip f a >=> flip f b >=> ... >=> flip f x >=> flip f y $ z
    
    The monadic effects of foldlM are sequenced from left to right. If at some step the bind operator (>>=) short-circuits (as with, e.g., mzero in a MonadPlus), the evaluated effects will be from an initial segment of the element sequence. If you want to evaluate the monadic effects in right-to-left order, or perhaps be able to short-circuit after processing a tail of the sequence of elements, you'll need to use foldrM instead. If the monadic effects don't short-circuit, the outermost application of f is to the rightmost element y, so that, ignoring effects, the result looks like a left fold:
    ((((z `f` a) `f` b) ... `f` w) `f` x) `f` y
    

    Examples

    Basic usage:
    >>> let f a e = do { print e ; return $ e : a }
    
    >>> foldlM f [] [0..3]
    0
    1
    2
    3
    [3,2,1,0]
    

  5. foldl1 :: Foldable1 t => (a -> a -> a) -> t a -> a

    base Data.Foldable1

    A variant of foldlMap1 where the leftmost element maps to itself.

  6. foldl1' :: Foldable1 t => (a -> a -> a) -> t a -> a

    base Data.Foldable1

    A variant of foldlMap1' where the leftmost element maps to itself.

  7. foldlM1 :: (Foldable1 t, Monad m) => (a -> a -> m a) -> t a -> m a

    base Data.Foldable1

    Monadic fold over the elements of a non-empty structure, associating to the left, i.e. from left to right.

  8. foldlMap1 :: Foldable1 t => (a -> b) -> (b -> a -> b) -> t a -> b

    base Data.Foldable1

    Left-associative fold of a structure, lazy in the accumulator. This is rarely what you want, but can work well for structures with efficient right-to-left sequencing and an operator that is lazy in its left argument. In case of NonEmpty lists, foldlMap1, when given a function f, a binary operator g, and a list, reduces the list using g from left to right applying f to the leftmost element:

    foldlMap1 f g (x1 :| [x2, ..., xn]) == (...(((f x1) `g` x2) `g`...) `g` xn
    
    Note that to produce the outermost application of the operator the entire input list must be traversed. This means that foldlMap1 will diverge if given an infinite list. If you want an efficient strict left-fold, you probably want to use foldlMap1' instead of foldlMap1. The reason for this is that the latter does not force the inner results (e.g. (f x1) `g` x2 in the above example) before applying them to the operator (e.g. to (`g` x3)). This results in a thunk chain <math> elements long, which then must be evaluated from the outside-in. For a general Foldable1 structure this should be semantically identical to:
    foldlMap1 f g = foldlMap1 f g . toNonEmpty
    

  9. foldlMap1' :: Foldable1 t => (a -> b) -> (b -> a -> b) -> t a -> b

    base Data.Foldable1

    Left-associative fold of a structure but with strict application of the operator. This ensures that each step of the fold is forced to Weak Head Normal Form before being applied, avoiding the collection of thunks that would otherwise occur. This is often what you want to strictly reduce a finite structure to a single strict result. For a general Foldable1 structure this should be semantically identical to:

    foldlMap1' f z = foldlMap1' f z . toNonEmpty
    

  10. foldlMapM1 :: (Foldable1 t, Monad m) => (a -> m b) -> (b -> a -> m b) -> t a -> m b

    base Data.Foldable1

    Map variant of foldlM1.

Page 22 of many | Previous | Next