reducers-3.12.1: Semigroups, specialized containers and a general map/reduce framework

Copyright | (c) Edward Kmett 2009
License | BSD3
Maintainer | ekmett@gmail.com
Stability | experimental
Portability | non-portable (MPTCs)
Safe Haskell | Trustworthy
Language | Haskell98

Data.Semigroup.Reducer

Description

A `c`-`Reducer` is a `Semigroup` with a canonical mapping from `c` to the Semigroup.

Synopsis

- class Semigroup m => Reducer c m where
  - unit :: c -> m
  - snoc :: m -> c -> m
  - cons :: c -> m -> m
- foldMapReduce :: (Foldable f, Monoid m, Reducer e m) => (a -> e) -> f a -> m
- foldMapReduce1 :: (Foldable1 f, Reducer e m) => (a -> e) -> f a -> m
- foldReduce :: (Foldable f, Monoid m, Reducer e m) => f e -> m
- foldReduce1 :: (Foldable1 f, Reducer e m) => f e -> m
- pureUnit :: (Applicative f, Reducer c n) => c -> f n
- returnUnit :: (Monad m, Reducer c n) => c -> m n
- newtype Count = Count {
  - getCount :: Int
  }

# Documentation

class Semigroup m => Reducer c m where Source

This type may be best read infix. A `c `Reducer` m` is a `Semigroup` `m` that maps values of type `c` through `unit` to values of type `m`. A `c`-`Reducer` may also supply operations which tack-on another `c` to an existing `Monoid` `m` on the left or right. These specialized reductions may be more efficient in some scenarios and are used when appropriate by a `Generator`. The names `cons` and `snoc` work by analogy to the synonymous operations in the list monoid.

This class deliberately avoids functional-dependencies, so that () can be a `c`-Reducer for all `c`, and so many common reducers can work over multiple types, for instance, First and Last may reduce both `a` and `Maybe` `a`. Since a `Generator` has a fixed element type, the input to the reducer is generally known and extracting from the monoid usually is sufficient to fix the result type. Combinators are available for most scenarios where this is not the case, and the few remaining cases can be handled by using an explicit type annotation.

Minimal definition: `unit`

Minimal complete definition

unit

Methods

unit :: c -> m Source

Convert a value into a `Semigroup`

snoc :: m -> c -> m Source

Append a value to a `Semigroup` for use in left-to-right reduction

cons :: c -> m -> m Source

Prepend a value onto a `Semigroup` for use during right-to-left reduction

Instances

Reducer Bool All Source |
Reducer Bool Any Source |
Reducer Int IntSet Source |
Reducer c () Source |
Reducer a Count Source |
Monoid m => Reducer m (WrappedMonoid m) Source |
Ord a => Reducer a (Set a) Source |
Reducer a (Seq a) Source |
Reducer a (Last a) Source |
Reducer a (First a) Source |
Ord a => Reducer a (Max a) Source |
Ord a => Reducer a (Min a) Source |
Num a => Reducer a (Product a) Source |
Num a => Reducer a (Sum a) Source |
Semigroup a => Reducer a (Dual a) Source |
Reducer c [c] Source |
HasUnion f => Reducer f (Union f) Source |
Semigroup m => Reducer m (Self m) Source |
Measured v a => Reducer a (FingerTree v a) Source |
(Reducer c m, Reducer c n) => Reducer c (m, n) Source |
(Reducer c m, Reducer c n, Reducer c o) => Reducer c (m, n, o) Source |
(Reducer c m, Reducer c n, Reducer c o, Reducer c p) => Reducer c (m, n, o, p) Source |
Applicative f => Reducer (f a) (Traversal f) Source |
Monad f => Reducer (f a) (Action f) Source |
Apply f => Reducer (f a) (Trav f) Source |
Reducer (Maybe a) (Last a) Source |
Reducer (Maybe a) (First a) Source |
(Applicative f, Reducer c m) => Reducer (f c) (Ap f m) Source |
Alternative f => Reducer (f a) (Alternate f a) Source |
(Monad f, Reducer c m) => Reducer (f c) (Mon f m) Source |
MonadPlus f => Reducer (f a) (MonadSum f a) Source |
(HasUnionWith f, Semigroup m, Monoid m) => Reducer (f m) (UnionWith f m) Source |
(Apply f, Reducer c m) => Reducer (f c) (App f m) Source |
Alt f => Reducer (f a) (Alter f a) Source |
Reducer c m => Reducer (WithReducer m c) m Source |
Reducer (a -> a) (Endo a) Source |
Reducer (Int, v) (IntMap v) Source |
Ord k => Reducer (k, v) (Map k v) Source |

foldMapReduce :: (Foldable f, Monoid m, Reducer e m) => (a -> e) -> f a -> m Source

Apply a `Reducer` to a `Foldable` container, after mapping the contents into a suitable form for reduction.

foldMapReduce1 :: (Foldable1 f, Reducer e m) => (a -> e) -> f a -> m Source

foldReduce :: (Foldable f, Monoid m, Reducer e m) => f e -> m Source

Apply a `Reducer` to a `Foldable` mapping each element through `unit`

foldReduce1 :: (Foldable1 f, Reducer e m) => f e -> m Source

Apply a `Reducer` to a `Foldable1` mapping each element through `unit`

pureUnit :: (Applicative f, Reducer c n) => c -> f n Source

returnUnit :: (Monad m, Reducer c n) => c -> m n Source

newtype Count Source

Constructors

Count |

Fields

getCount :: Int

Instances

Eq Count Source |
Data Count Source |
Ord Count Source |
Read Count Source |
Show Count Source |
Monoid Count Source |
Hashable Count Source |
Semigroup Count Source |
Reducer a Count Source |