deferred-folds-0.9.18.7: Abstractions over deferred folds

Safe Haskell | None
Language | Haskell2010

DeferredFolds.Unfoldl

Synopsis

- filter :: (a -> Bool) -> Unfoldl a -> Unfoldl a
- fold :: Fold input output -> Unfoldl input -> output
- newtype Unfoldl a = Unfoldl (forall x. (x -> a -> x) -> x -> x)
- foldable :: Foldable foldable => foldable a -> Unfoldl a
- intsInRange :: Int -> Int -> Unfoldl Int
- byteStringBytes :: ByteString -> Unfoldl Word8
- shortByteStringBytes :: ShortByteString -> Unfoldl Word8
- primArray :: Prim prim => PrimArray prim -> Unfoldl prim
- primArrayWithIndices :: Prim prim => PrimArray prim -> Unfoldl (Int, prim)
- unfoldlM :: UnfoldlM Identity input -> Unfoldl input
- mapFoldInput :: (forall x. Fold b x -> Fold a x) -> Unfoldl a -> Unfoldl b
- mapAssocs :: Map key value -> Unfoldl (key, value)
- intMapAssocs :: IntMap value -> Unfoldl (Int, value)

# Documentation

filter :: (a -> Bool) -> Unfoldl a -> Unfoldl a Source #

Filter the values given a predicate

fold :: Fold input output -> Unfoldl input -> output Source #

Apply a Gonzalez fold

newtype Unfoldl a Source #

A projection on data, which only knows how to execute a strict left-fold.

It is a monad and a monoid, and is very useful for efficiently aggregating the projections on data intended for left-folding, since its concatenation (`<>`) has complexity of `O(1)`.

Intuition

The intuition for this abstraction can be derived from lists.

Let's consider the `foldl'` function for lists:

    foldl' :: (b -> a -> b) -> b -> [a] -> b

If we rearrange its parameters we get

    foldl' :: [a] -> (b -> a -> b) -> b -> b

Which in Haskell is essentially the same as

    foldl' :: [a] -> (forall b. (b -> a -> b) -> b -> b)

We can isolate that part into an abstraction:

    newtype Unfoldl a = Unfoldl (forall b. (b -> a -> b) -> b -> b)

Then we get to this simple morphism:

    list :: [a] -> Unfoldl a
    list list = Unfoldl (\ step init -> foldl' step init list)

We can do the same with say Data.Text.Text:

    text :: Text -> Unfoldl Char
    text text = Unfoldl (\ step init -> Data.Text.foldl' step init text)

And then we can use those both to concatenate with just an `O(1)` cost:

    abcdef :: Unfoldl Char
    abcdef = list ['a', 'b', 'c'] <> text "def"

Please notice that up until this moment no actual data materialization has happened and hence no traversals have appeared. All that we've done is just composed a function, which only specifies which parts of data structures to traverse to perform a left-fold. Only at the moment where the actual folding will happen will we actually traverse the source data. E.g., using the "fold" function:

    abcdefLength :: Int
    abcdefLength = fold Control.Foldl.length abcdef

Constructors

Unfoldl (forall x. (x -> a -> x) -> x -> x) |

#### Instances

Instances details

Alternative Unfoldl Source # |
Instance details Defined in DeferredFolds.Defs.Unfoldl Methods empty :: Unfoldl a Source # (<|>) :: Unfoldl a -> Unfoldl a -> Unfoldl a Source # some :: Unfoldl a -> Unfoldl [a] Source # many :: Unfoldl a -> Unfoldl [a] Source #
Applicative Unfoldl Source # |
Instance details Defined in DeferredFolds.Defs.Unfoldl Methods pure :: a -> Unfoldl a Source # (<*>) :: Unfoldl (a -> b) -> Unfoldl a -> Unfoldl b Source # liftA2 :: (a -> b -> c) -> Unfoldl a -> Unfoldl b -> Unfoldl c Source # (*>) :: Unfoldl a -> Unfoldl b -> Unfoldl b Source # (<*) :: Unfoldl a -> Unfoldl b -> Unfoldl a Source #
Functor Unfoldl Source # |
Instance details Defined in DeferredFolds.Defs.Unfoldl Methods fmap :: (a -> b) -> Unfoldl a -> Unfoldl b Source # (<$) :: a -> Unfoldl b -> Unfoldl a Source #
Monad Unfoldl Source # |
Instance details Defined in DeferredFolds.Defs.Unfoldl Methods (>>=) :: Unfoldl a -> (a -> Unfoldl b) -> Unfoldl b Source # (>>) :: Unfoldl a -> Unfoldl b -> Unfoldl b Source # return :: a -> Unfoldl a Source #
MonadPlus Unfoldl Source # |
Instance details Defined in DeferredFolds.Defs.Unfoldl Methods mzero :: Unfoldl a Source # mplus :: Unfoldl a -> Unfoldl a -> Unfoldl a Source #
Foldable Unfoldl Source # |

Instance details

Defined in DeferredFolds.Defs.Unfoldl

Methods

fold :: Monoid m => Unfoldl m -> m Source #

foldMap :: Monoid m => (a -> m) -> Unfoldl a -> m Source #

foldMap' :: Monoid m => (a -> m) -> Unfoldl a -> m Source #

foldr :: (a -> b -> b) -> b -> Unfoldl a -> b Source #

foldr' :: (a -> b -> b) -> b -> Unfoldl a -> b Source #

foldl :: (b -> a -> b) -> b -> Unfoldl a -> b Source #

foldl' :: (b -> a -> b) -> b -> Unfoldl a -> b Source #

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

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

toList :: Unfoldl a -> [a] Source #

null :: Unfoldl a -> Bool Source #

length :: Unfoldl a -> Int Source #

elem :: Eq a => a -> Unfoldl a -> Bool Source #

maximum :: Ord a => Unfoldl a -> a Source #

minimum :: Ord a => Unfoldl a -> a Source #

sum :: Num a => Unfoldl a -> a Source #

product :: Num a => Unfoldl a -> a Source #

Monoid (Unfoldl a) Source # |
Instance details Defined in DeferredFolds.Defs.Unfoldl Methods mempty :: Unfoldl a Source # mappend :: Unfoldl a -> Unfoldl a -> Unfoldl a Source # mconcat :: [Unfoldl a] -> Unfoldl a Source #
Semigroup (Unfoldl a) Source # |
Instance details Defined in DeferredFolds.Defs.Unfoldl Methods (<>) :: Unfoldl a -> Unfoldl a -> Unfoldl a Source # sconcat :: NonEmpty (Unfoldl a) -> Unfoldl a Source # stimes :: Integral b => b -> Unfoldl a -> Unfoldl a Source #
IsList (Unfoldl a) Source # |

Instance details

Defined in DeferredFolds.Defs.Unfoldl

Associated Types

type Item (Unfoldl a) |
Instance details Defined in DeferredFolds.Defs.Unfoldl type Item (Unfoldl a) = a

Methods

fromList :: [Item (Unfoldl a)] -> Unfoldl a Source #

fromListN :: Int -> [Item (Unfoldl a)] -> Unfoldl a Source #

toList :: Unfoldl a -> [Item (Unfoldl a)] Source #

Show a => Show (Unfoldl a) Source # |
Instance details Defined in DeferredFolds.Defs.Unfoldl Methods showsPrec :: Int -> Unfoldl a -> ShowS Source # show :: Unfoldl a -> String Source # showList :: [Unfoldl a] -> ShowS Source #
Eq a => Eq (Unfoldl a) Source # |
Instance details Defined in DeferredFolds.Defs.Unfoldl Methods (==) :: Unfoldl a -> Unfoldl a -> Bool Source # (/=) :: Unfoldl a -> Unfoldl a -> Bool Source #
type Item (Unfoldl a) Source # |
Instance details Defined in DeferredFolds.Defs.Unfoldl type Item (Unfoldl a) = a

foldable :: Foldable foldable => foldable a -> Unfoldl a Source #

Construct from any foldable

intsInRange :: Int -> Int -> Unfoldl Int Source #

Ints in the specified inclusive range

byteStringBytes :: ByteString -> Unfoldl Word8 Source #

Bytes of a bytestring

shortByteStringBytes :: ShortByteString -> Unfoldl Word8 Source #

Bytes of a short bytestring

primArray :: Prim prim => PrimArray prim -> Unfoldl prim Source #

Elements of a prim array

primArrayWithIndices :: Prim prim => PrimArray prim -> Unfoldl (Int, prim) Source #

Elements of a prim array coming paired with indices

unfoldlM :: UnfoldlM Identity input -> Unfoldl input Source #

Unlift a monadic unfold

mapFoldInput :: (forall x. Fold b x -> Fold a x) -> Unfoldl a -> Unfoldl b Source #

Lift a fold input mapping function into a mapping of unfolds

mapAssocs :: Map key value -> Unfoldl (key, value) Source #

Associations of a map

intMapAssocs :: IntMap value -> Unfoldl (Int, value) Source #

Associations of an intmap