Safe Haskell | Safe |
---|---|

Language | Haskell2010 |

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.

- sum :: Num a => L' a a
- product :: Num a => L' a a
- count :: Enum e => L' a e
- mconcat :: Monoid m => L' m m
- minimum :: Ord a => L' a (Maybe a)
- maximum :: Ord a => L' a (Maybe a)
- nub :: Ord a => L' a [a]
- slowNub :: Eq a => L' a [a]
- intoSet :: Ord a => L' a (Set a)
- last :: L' a (Maybe a)
- nth :: (Eq b, Ord b, Num b) => b -> L' a (Maybe a)

# Documentation

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

Count the number of elements fed to a fold

`>>>`

10`run [1 .. 10] count`

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.

`>>>`

[1, 2, 3, 4, 5, 6, 7, 8]`run [[1, 2, 3, 4], [5, 6, 7, 8]] mconcat`

`>>>`

Sum {getSum = 10}`run (map Sum [1, 2, 3, 4]) mconcat`

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

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

.

`>>>`

1`run [1, 2, 3] minimum`

`>>>`

... diverges ...`run [1 ..] minimum`

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

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

.

`>>>`

3`run [1, 2, 3] maximum`

`>>>`

... diverges ...`run [1 ..] maximum`

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

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

`>>>`

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

`>>>`

[1, 2]`run [1, 2, 1] nub`

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

Collect all members into a `Set`

.

`>>>`

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

Grab the last element inputted

`>>>`

Just 10`run [1 .. 10] last`

`>>>`

Nothing`run [] last`