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

Safe HaskellSafe
LanguageHaskell2010

Data.Fold.Common.L'

Description

A collection of common left folds. Note that all of these are strict and do not short circuit. These are useful for operations that require inspecting the entire list to calculate the final state.

Synopsis

Documentation

sum :: Num a => L' a a Source

Sum of the inputs

>>> run [1 .. 10] sum
55

product :: Num a => L' a a Source

Product of the input

>>> run [1 .. 10] product
3628800

count :: Enum e => L' a e Source

Count the number of elements fed to a fold

>>> run [1 .. 10] count
10

Note: GHCi will default Enum e to (). If you see

*** Exception: Prelude.Enum.().succ: bad argument

You've been bitten by this.

mconcat :: Monoid m => L' m m Source

mappend all the elements of a sequence together.

>>> run [[1, 2, 3, 4], [5, 6, 7, 8]] mconcat
[1, 2, 3, 4, 5, 6, 7, 8]
>>> run (map Sum [1, 2, 3, 4]) mconcat
Sum {getSum = 10}

minimum :: Ord a => L' a (Maybe a) Source

Minimum of all inputs. If no inputs are supplied this returns Nothing.

>>> run [1, 2, 3] minimum
1
>>> run [1 ..] minimum
... diverges ...

maximum :: Ord a => L' a (Maybe a) Source

Maximum of all inputs. If no inputs are supplied this returns Nothing.

>>> run [1, 2, 3] maximum
3
>>> run [1 ..] maximum
... diverges ...

nub :: Ord a => L' a [a] Source

De-duplicate all the inputs while preserving order. O(n log(n))

>>> run (replicate 10 1 ++ replicate 10 2) nub
[1, 2]
>>> run [1, 2, 1] nub
[1, 2]

slowNub :: Eq a => L' a [a] Source

De-duplicate all the inputs while preserving order. O(n^2). This should be equivalent (but slower) then nub for Ord types.

>>> run (replicate 10 1 ++ replicate 10 2) slowNub
[1, 2]
>>> run [1, 2, 1] slowNub
[1, 2]

intoSet :: Ord a => L' a (Set a) Source

Collect all members into a Set.

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

last :: L' a (Maybe a) Source

Grab the last element inputted

>>> run [1 .. 10] last
Just 10
>>> run [] last
Nothing

nth :: (Eq b, Ord b, Num b) => b -> L' a (Maybe a) Source

Grab the nth element inputted.

>>> run [1 .. 10] (nth 5)
Just 6
>>> run [1 .. 10] (nth 20)
Nothing