folds-common-0.2.0.0: A playground of common folds for folds

Safe HaskellSafe
LanguageHaskell2010

Data.Fold.Common.R

Description

A series of common right folds. These tend to be list centric since since lists provide such a lousy monoid.

Synopsis

Documentation

intoList :: R a [a] Source

An extremely boring fold. You can almost view this as an identity fold across lists.

>>> run [1 .. 10] intoLists
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

take :: (Eq b, Ord b, Num b) => b -> R a [a] Source

Take the first n inputs to the fold. If less then n inputs are fed in total then take as many as possible.

>>> run [1 .. 10] (take 3)
[1, 2, 3]
>>> run [1, 2, 3] (take 100)
[1, 2, 3]

drop :: (Eq b, Ord b, Num b) => b -> R a [a] Source

Drop the first n items. If less then n items are supplied then return the empty list.

>>> run [1, 2, 3] (drop 1)
[2, 3]
>>> run [1, 2, 3] (drop 100)
[]

indexOf :: Enum e => (a -> Bool) -> R a (Maybe e) Source

Find the first index for which a predicate holds.

>>> run [1, 2, 3, 4] (indexOf (== 4))
Just 3
>>> run [1, 2, 3, 4] (indexOf (> 4))
Nothing

chunk :: (Show b, Eq b) => (a -> b) -> R a [[a]] Source

Chunk the input into partitions according to a function. While the values from the function are equal elements are collected into a chunk. Note that partitioning according to a predicate is just a special case of this.

>>> run [1, 1, 2, 3] (chunk id)
[[1, 1], [2], [3]]
>>> run [1, -1, 2, 1] (chunk abs)
[[1, -1], 2, [1]]
>>> run [1, 2, 4, 6, 5] (chunk even)
[[1], [2, 4, 6], 5]

concat :: R [a] [a] Source

Lazily produce a flattened list of the inputted lists.

>>> run [[1], [2], [3]] concat
[1, 2, 3]
>>> head $ run (map return [1..]) concat
1

Note: The right fold ensures that all applications of ++ associate to the right. This makes this fold ideal for streaming but slow when completely forced.