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

Copyright Edward Kmett BSD-style (see the LICENSE file in the distribution) libraries@haskell.org experimental portable Trustworthy Haskell2010

Data.Semifoldable

Description

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

Synopsis

# Documentation

class Foldable t => Semifoldable t where Source #

Non-empty data structures that can be folded.

Minimal complete definition

Methods

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

Combine the elements of a structure using a semigroup.

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

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

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


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

A variant of semifoldMap that is strict in the accumulator.

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


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

Right-associative fold of a structure.

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

semifoldr 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, semifoldr can produce a terminating expression from an infinite list.

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

semifoldr f = semifoldr f . toNonEmpty

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

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

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

Left-associative fold of a structure.

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

semifoldl 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 semifoldl will diverge if given an infinite list.

Also note that if you want an efficient left-fold, you probably want to use semifoldl' instead of semifoldl. 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 Semifoldable structure this should be semantically identical to,

semifoldl f z = semifoldl f . toNonEmpty

semifoldl' :: (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 Semifoldable structure this should be semantically identical to,

semifoldl' f z = semifoldl f . toNonEmpty

toNonEmpty :: t a -> NonEmpty a Source #

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

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


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

The largest element of a non-empty structure.

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


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

The least element of a non-empty structure.

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


semihead :: t a -> a Source #

The first element of a non-empty structure.

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


semilast :: t a -> a Source #

The last element of a non-empty structure.

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


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

For Functors, semifoldrMap f g = semifoldr g . fmap g.

semifoldl'Map :: (a -> b) -> (b -> a -> b) -> t a -> b Source #

For Functors, semifoldl'Map f g = semifoldl' g . fmap g.

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

For Functors, semifoldlMap f g = semifoldl g . fmap g.

semifoldr'Map :: (a -> b) -> (a -> b -> b) -> t a -> b Source #

For Functors, semifoldr'Map f g = semifoldr' g . fmap g.

Instances

intercalate1 :: (Semifoldable 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?"


semifoldrM :: (Semifoldable 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.

semifoldlM :: (Semifoldable 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.

semifoldrMapM :: (Semifoldable t, Monad m) => (a -> m b) -> (a -> b -> m b) -> t a -> m b Source #

Map variant of semifoldrM.

semifoldlMapM :: (Semifoldable t, Monad m) => (a -> m b) -> (b -> a -> m b) -> t a -> m b Source #

Map variant of semifoldlM.

semimaximumBy :: Semifoldable t => (a -> a -> Ordering) -> t a -> a Source #

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

semiminimumBy :: Semifoldable t => (a -> a -> Ordering) -> t a -> a Source #

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