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
>>>
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]
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]
Grab the last element inputted
>>>
run [1 .. 10] last
Just 10
>>>
run [] last
Nothing