foldable1-0.2: Class of non-empty data structures that can be folded to a summary value.

Data.Foldable1

Description

class of non-empty data structures that can be folded to a summary value.

Synopsis

# Documentation

class Foldable t => Foldable1 t where Source #

Non-empty data structures that can be folded.

Minimal complete definition

Methods

fold1 :: Semigroup m => t m -> m Source #

Combine the elements of a structure using a semigroup.

foldMap1 :: Semigroup m => (a -> m) -> t a -> m Source #

Map each element of the structure to a semigroup, and combine the results.

>>> foldMap1 Sum (1 :| [2, 3, 4])
Sum {getSum = 10}


foldMap1' :: Semigroup m => (a -> m) -> t a -> m Source #

A variant of foldMap1 that is strict in the accumulator.

>>> foldMap1' Sum (1 :| [2, 3, 4])
Sum {getSum = 10}


foldr1 :: (a -> a -> a) -> t a -> a Source #

Right-associative fold of a structure.

In the case of lists, foldr1, when applied to a binary operator, and a list, reduces the list using the binary operator, from right to left:

foldr1 f [x1, x2, ..., xn] == x1 f (x2 f ... (xn1 f xn )...)

Note that, since the head of the resulting expression is produced by an application of the operator to the first element of the list, foldr1 can produce a terminating expression from an infinite list.

For a general Foldable1 structure this should be semantically identical to,

foldr1 f = foldr1 f . toNonEmpty

foldr1' :: (a -> a -> a) -> t a -> a Source #

Right-associative fold of a structure, but with strict application of the operator.

foldl1 :: (a -> a -> a) -> t a -> a Source #

Left-associative fold of a structure.

In the case of lists, foldl1, when applied to a binary operator, and a list, reduces the list using the binary operator, from left to right:

foldl1 f [x1, x2, ..., xn] == (...((x1 f x2) f...) f xn

Note that to produce the outermost application of the operator the entire input list must be traversed. This means that foldl1 will diverge if given an infinite list.

Also note that if you want an efficient left-fold, you probably want to use foldl1' instead of foldl1. The reason for this is that latter does not force the "inner" results (e.g. x1 f x2 in the above example) before applying them to the operator (e.g. to (f x3)). This results in a thunk chain $$\mathcal{O}(n)$$ elements long, which then must be evaluated from the outside-in.

For a general Foldable1 structure this should be semantically identical to,

foldl1 f z = foldl1 f . toNonEmpty

foldl1' :: (a -> a -> a) -> t a -> a Source #

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 list to a single, monolithic result (e.g. length).

For a general Foldable1 structure this should be semantically identical to,

foldl1' f z = foldl1 f . toNonEmpty

toNonEmpty :: t a -> NonEmpty a Source #

List of elements of a structure, from left to right.

>>> toNonEmpty (Identity 2)
2 :| []


maximum1 :: forall a. Ord a => t a -> a Source #

The largest element of a non-empty structure.

>>> maximum1 (32 :| [64, 8, 128, 16])
128


minimum1 :: forall a. Ord a => t a -> a Source #

The least element of a non-empty structure.

>>> minimum1 (32 :| [64, 8, 128, 16])
8


head1 :: t a -> a Source #

The first element of a non-empty structure.

>>> head1 (1 :| [2, 3, 4])
1


last1 :: t a -> a Source #

The last element of a non-empty structure.

>>> last1 (1 :| [2, 3, 4])
4


foldrMap1 :: (a -> b) -> (a -> b -> b) -> t a -> b Source #

For Functors, foldrMap1 f g = foldr1 g . fmap g.

foldl'Map1 :: (a -> b) -> (b -> a -> b) -> t a -> b Source #

For Functors, foldl'Map1 f g = foldl1' g . fmap g.

foldlMap1 :: (a -> b) -> (b -> a -> b) -> t a -> b Source #

For Functors, foldlMap1 f g = foldl1 g . fmap g.

foldr'Map1 :: (a -> b) -> (a -> b -> b) -> t a -> b Source #

For Functors, foldr'Map1 f g = foldr1' g . fmap g.

Instances

intercalate1 :: (Foldable1 t, Semigroup m) => m -> t m -> m Source #

Insert an m between each pair of t m.

>>> intercalate1 ", " $"hello" :| ["how", "are", "you"] "hello, how, are, you"  >>> intercalate1 ", "$ "hello" :| []
"hello"

>>> intercalate1 mempty \$ "I" :| ["Am", "Fine", "You?"]
"IAmFineYou?"


foldrM1 :: (Foldable1 t, Monad m) => (a -> a -> m a) -> t a -> m a Source #

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

foldlM1 :: (Foldable1 t, Monad m) => (a -> a -> m a) -> t a -> m a Source #

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

foldrMapM1 :: (Foldable1 t, Monad m) => (a -> m b) -> (a -> b -> m b) -> t a -> m b Source #

Map variant of foldrM1.

foldlMapM1 :: (Foldable1 t, Monad m) => (a -> m b) -> (b -> a -> m b) -> t a -> m b Source #

Map variant of foldlM1.

maximum1By :: Foldable1 t => (a -> a -> Ordering) -> t a -> a Source #

The largest element of a non-empty structure with respect to the given comparison function.

minimum1By :: Foldable1 t => (a -> a -> Ordering) -> t a -> a Source #

The least element of a non-empty structure with respect to the given comparison function.