foldl-1.4.5: Composable, streaming, and efficient left folds

Safe Haskell | None
Language | Haskell98

Control.Scanl

Contents

- Scan Types
- Scanning
- Utilities

Description

This module provides efficient and streaming left map-with-accumulator that you can combine using `Applicative` style.

Import this module qualified to avoid clashing with the Prelude:

    >>> import qualified Control.Scanl as SL

Use `scan` to apply a `Fold` to a list (or other `Traversable` structures) from left to right, and `scanr` to do so from right to left.

Synopsis

- data Scan a b = Scan (a -> State x b) x
- data ScanM m a b = ScanM (a -> StateT x m b) (m x)
- scan :: Traversable t => Scan a b -> t a -> t b
- scanM :: (Traversable t, Monad m) => ScanM m a b -> t a -> m (t b)
- scanr :: Traversable t => Scan a b -> t a -> t b
- prescan :: Fold a b -> Scan a b
- postscan :: Fold a b -> Scan a b
- purely :: (forall x. (a -> State x b) -> x -> r) -> Scan a b -> r
- purely_ :: (forall x. (x -> a -> (x, b)) -> x -> r) -> Scan a b -> r
- impurely :: (forall x. (a -> StateT x m b) -> m x -> r) -> ScanM m a b -> r
- impurely_ :: Monad m => (forall x. (x -> a -> m (x, b)) -> m x -> r) -> ScanM m a b -> r
- generalize :: Monad m => Scan a b -> ScanM m a b
- simplify :: ScanM Identity a b -> Scan a b
- hoists :: (forall x. m x -> n x) -> ScanM m a b -> ScanM n a b
- arrM :: Monad m => (b -> m c) -> ScanM m b c
- premap :: (a -> b) -> Scan b r -> Scan a r
- premapM :: Monad m => (a -> m b) -> ScanM m b r -> ScanM m a r

# Scan Types

data Scan a b Source #

Efficient representation of a left map-with-accumulator that preserves the scan's step function and initial accumulator.

This allows the `Applicative` instance to assemble derived scans that traverse the container only once

A '`Scan` a b' processes elements of type a replacing each with a value of type b.

Constructors

Scan (a -> State x b) x | `Scan` `step` `initial`

Instances

Arrow Scan Source # |
Instance details Defined in Control.Scanl Methods arr :: (b -> c) -> Scan b c Source # first :: Scan b c -> Scan (b, d) (c, d) Source # second :: Scan b c -> Scan (d, b) (d, c) Source # (***) :: Scan b c -> Scan b' c' -> Scan (b, b') (c, c') Source # (&&&) :: Scan b c -> Scan b c' -> Scan b (c, c') Source #
Profunctor Scan Source # |
Instance details Defined in Control.Scanl Methods dimap :: (a -> b) -> (c -> d) -> Scan b c -> Scan a d Source # lmap :: (a -> b) -> Scan b c -> Scan a c Source # rmap :: (b -> c) -> Scan a b -> Scan a c Source # (#.) :: Coercible c b => q b c -> Scan a b -> Scan a c Source # (.#) :: Coercible b a => Scan b c -> q a b -> Scan a c Source #
Functor (Scan a) Source # |
Instance details Defined in Control.Scanl Methods fmap :: (a0 -> b) -> Scan a a0 -> Scan a b Source # (<$) :: a0 -> Scan a b -> Scan a a0 Source #
Applicative (Scan a) Source # |
Instance details Defined in Control.Scanl Methods pure :: a0 -> Scan a a0 Source # (<*>) :: Scan a (a0 -> b) -> Scan a a0 -> Scan a b Source # liftA2 :: (a0 -> b -> c) -> Scan a a0 -> Scan a b -> Scan a c Source # (*>) :: Scan a a0 -> Scan a b -> Scan a b Source # (<*) :: Scan a a0 -> Scan a b -> Scan a a0 Source #
Category Scan Source # |
Instance details Defined in Control.Scanl Methods id :: Scan a a Source # (.) :: Scan b c -> Scan a b -> Scan a c Source #
Floating b => Floating (Scan a b) Source # |

Instance details

Defined in Control.Scanl

Methods

pi :: Scan a b Source #

exp :: Scan a b -> Scan a b Source #

log :: Scan a b -> Scan a b Source #

sqrt :: Scan a b -> Scan a b Source #

(**) :: Scan a b -> Scan a b -> Scan a b Source #

logBase :: Scan a b -> Scan a b -> Scan a b Source #

sin :: Scan a b -> Scan a b Source #

cos :: Scan a b -> Scan a b Source #

tan :: Scan a b -> Scan a b Source #

asin :: Scan a b -> Scan a b Source #

acos :: Scan a b -> Scan a b Source #

atan :: Scan a b -> Scan a b Source #

sinh :: Scan a b -> Scan a b Source #

cosh :: Scan a b -> Scan a b Source #

tanh :: Scan a b -> Scan a b Source #

asinh :: Scan a b -> Scan a b Source #

acosh :: Scan a b -> Scan a b Source #

atanh :: Scan a b -> Scan a b Source #

log1p :: Scan a b -> Scan a b Source #

expm1 :: Scan a b -> Scan a b Source #

log1pexp :: Scan a b -> Scan a b Source #

log1mexp :: Scan a b -> Scan a b Source #

Fractional b => Fractional (Scan a b) Source # |
Instance details Defined in Control.Scanl Methods (/) :: Scan a b -> Scan a b -> Scan a b Source # recip :: Scan a b -> Scan a b Source # fromRational :: Rational -> Scan a b Source #
Num b => Num (Scan a b) Source # |
Instance details Defined in Control.Scanl Methods (+) :: Scan a b -> Scan a b -> Scan a b Source # (-) :: Scan a b -> Scan a b -> Scan a b Source # (*) :: Scan a b -> Scan a b -> Scan a b Source # negate :: Scan a b -> Scan a b Source # abs :: Scan a b -> Scan a b Source # signum :: Scan a b -> Scan a b Source # fromInteger :: Integer -> Scan a b Source #
Semigroup b => Semigroup (Scan a b) Source # |
Instance details Defined in Control.Scanl Methods (<>) :: Scan a b -> Scan a b -> Scan a b Source # sconcat :: NonEmpty (Scan a b) -> Scan a b Source # stimes :: Integral b0 => b0 -> Scan a b -> Scan a b Source #
Monoid b => Monoid (Scan a b) Source # |
Instance details Defined in Control.Scanl Methods mempty :: Scan a b Source # mappend :: Scan a b -> Scan a b -> Scan a b Source # mconcat :: [Scan a b] -> Scan a b Source #

data ScanM m a b Source #

Like `Scan`, but monadic.

A '`ScanM` m a b' processes elements of type a and results in a monadic value of type m b.

Constructors

ScanM (a -> StateT x m b) (m x) | `ScanM` `step` `initial` `extract`

Instances

Monad m => Arrow (ScanM m) Source # |
Instance details Defined in Control.Scanl Methods arr :: (b -> c) -> ScanM m b c Source # first :: ScanM m b c -> ScanM m (b, d) (c, d) Source # second :: ScanM m b c -> ScanM m (d, b) (d, c) Source # (***) :: ScanM m b c -> ScanM m b' c' -> ScanM m (b, b') (c, c') Source # (&&&) :: ScanM m b c -> ScanM m b c' -> ScanM m b (c, c') Source #
Functor m => Profunctor (ScanM m) Source # |
Instance details Defined in Control.Scanl Methods dimap :: (a -> b) -> (c -> d) -> ScanM m b c -> ScanM m a d Source # lmap :: (a -> b) -> ScanM m b c -> ScanM m a c Source # rmap :: (b -> c) -> ScanM m a b -> ScanM m a c Source # (#.) :: Coercible c b => q b c -> ScanM m a b -> ScanM m a c Source # (.#) :: Coercible b a => ScanM m b c -> q a b -> ScanM m a c Source #
Monad m => Category (ScanM m :: Type -> Type -> Type) Source # |
Instance details Defined in Control.Scanl Methods id :: ScanM m a a Source # (.) :: ScanM m b c -> ScanM m a b -> ScanM m a c Source #
Functor m => Functor (ScanM m a) Source # |
Instance details Defined in Control.Scanl Methods fmap :: (a0 -> b) -> ScanM m a a0 -> ScanM m a b Source # (<$) :: a0 -> ScanM m a b -> ScanM m a a0 Source #
Applicative m => Applicative (ScanM m a) Source # |
Instance details Defined in Control.Scanl Methods pure :: a0 -> ScanM m a a0 Source # (<*>) :: ScanM m a (a0 -> b) -> ScanM m a a0 -> ScanM m a b Source # liftA2 :: (a0 -> b -> c) -> ScanM m a a0 -> ScanM m a b -> ScanM m a c Source # (*>) :: ScanM m a a0 -> ScanM m a b -> ScanM m a b Source # (<*) :: ScanM m a a0 -> ScanM m a b -> ScanM m a a0 Source #
(Monad m, Floating b) => Floating (ScanM m a b) Source # |

Instance details

Defined in Control.Scanl

Methods

pi :: ScanM m a b Source #

exp :: ScanM m a b -> ScanM m a b Source #

log :: ScanM m a b -> ScanM m a b Source #

sqrt :: ScanM m a b -> ScanM m a b Source #

(**) :: ScanM m a b -> ScanM m a b -> ScanM m a b Source #

logBase :: ScanM m a b -> ScanM m a b -> ScanM m a b Source #

sin :: ScanM m a b -> ScanM m a b Source #

cos :: ScanM m a b -> ScanM m a b Source #

tan :: ScanM m a b -> ScanM m a b Source #

asin :: ScanM m a b -> ScanM m a b Source #

acos :: ScanM m a b -> ScanM m a b Source #

atan :: ScanM m a b -> ScanM m a b Source #

sinh :: ScanM m a b -> ScanM m a b Source #

cosh :: ScanM m a b -> ScanM m a b Source #

tanh :: ScanM m a b -> ScanM m a b Source #

asinh :: ScanM m a b -> ScanM m a b Source #

acosh :: ScanM m a b -> ScanM m a b Source #

atanh :: ScanM m a b -> ScanM m a b Source #

log1p :: ScanM m a b -> ScanM m a b Source #

expm1 :: ScanM m a b -> ScanM m a b Source #

log1pexp :: ScanM m a b -> ScanM m a b Source #

log1mexp :: ScanM m a b -> ScanM m a b Source #

(Monad m, Fractional b) => Fractional (ScanM m a b) Source # |
Instance details Defined in Control.Scanl Methods (/) :: ScanM m a b -> ScanM m a b -> ScanM m a b Source # recip :: ScanM m a b -> ScanM m a b Source # fromRational :: Rational -> ScanM m a b Source #
(Monad m, Num b) => Num (ScanM m a b) Source # |

Instance details

Defined in Control.Scanl

Methods

(+) :: ScanM m a b -> ScanM m a b -> ScanM m a b Source #

(-) :: ScanM m a b -> ScanM m a b -> ScanM m a b Source #

(*) :: ScanM m a b -> ScanM m a b -> ScanM m a b Source #

negate :: ScanM m a b -> ScanM m a b Source #

abs :: ScanM m a b -> ScanM m a b Source #

signum :: ScanM m a b -> ScanM m a b Source #

fromInteger :: Integer -> ScanM m a b Source #

(Monad m, Semigroup b) => Semigroup (ScanM m a b) Source # |
Instance details Defined in Control.Scanl Methods (<>) :: ScanM m a b -> ScanM m a b -> ScanM m a b Source # sconcat :: NonEmpty (ScanM m a b) -> ScanM m a b Source # stimes :: Integral b0 => b0 -> ScanM m a b -> ScanM m a b Source #
(Monad m, Monoid b) => Monoid (ScanM m a b) Source # |
Instance details Defined in Control.Scanl Methods mempty :: ScanM m a b Source # mappend :: ScanM m a b -> ScanM m a b -> ScanM m a b Source # mconcat :: [ScanM m a b] -> ScanM m a b Source #

# Scanning

scan :: Traversable t => Scan a b -> t a -> t b Source #

Apply a strict left `Scan` to a `Traversable` container

scanM :: (Traversable t, Monad m) => ScanM m a b -> t a -> m (t b) Source #

Like `scan` but monadic

scanr :: Traversable t => Scan a b -> t a -> t b Source #

Like `scan` but start scanning from the right

prescan :: Fold a b -> Scan a b Source #

Convert a `Fold` into a prescan

"Prescan" means that the last element of the scan is not included

postscan :: Fold a b -> Scan a b Source #

Convert a `Fold` into a postscan

"Postscan" means that the first element of the scan is not included

# Utilities

purely :: (forall x. (a -> State x b) -> x -> r) -> Scan a b -> r Source #

Upgrade a scan to accept the `Scan` type

purely_ :: (forall x. (x -> a -> (x, b)) -> x -> r) -> Scan a b -> r Source #

Upgrade a more traditional scan to accept the `Scan` type

impurely :: (forall x. (a -> StateT x m b) -> m x -> r) -> ScanM m a b -> r Source #

Upgrade a monadic scan to accept the `ScanM` type

impurely_ :: Monad m => (forall x. (x -> a -> m (x, b)) -> m x -> r) -> ScanM m a b -> r Source #

Upgrade a more traditional monadic scan to accept the `ScanM` type

generalize :: Monad m => Scan a b -> ScanM m a b Source #

Generalize a `Scan` to a `ScanM`

    generalize (pure r) = pure r

    generalize (f <*> x) = generalize f <*> generalize x

simplify :: ScanM Identity a b -> Scan a b Source #

Simplify a pure `ScanM` to a `Scan`

    simplify (pure r) = pure r

    simplify (f <*> x) = simplify f <*> simplify x

hoists :: (forall x. m x -> n x) -> ScanM m a b -> ScanM n a b Source #

Shift a `ScanM` from one monad to another with a morphism such as `lift` or `liftIO`; the effect is the same as `hoist`.

arrM :: Monad m => (b -> m c) -> ScanM m b c Source #

premap :: (a -> b) -> Scan b r -> Scan a r Source #

`(premap f scaner)` returns a new `Scan` where f is applied at each step

    scan (premap f scaner) list = scan scaner (map f list)

    premap id = id

    premap (f . g) = premap g . premap f

    premap k (pure r) = pure r

    premap k (f <*> x) = premap k f <*> premap k x

premapM :: Monad m => (a -> m b) -> ScanM m b r -> ScanM m a r Source #

`(premapM f scaner)` returns a new `ScanM` where f is applied to each input element

    premapM return = id

    premapM (f <=< g) = premap g . premap f

    premapM k (pure r) = pure r

    premapM k (f <*> x) = premapM k f <*> premapM k x