Copyright | (c) Donnacha Oisín Kidney 2018 |
---|---|
License | MIT |
Maintainer | mail@doisinkidney.com |
Stability | experimental |
Portability | portable |
Safe Haskell | Trustworthy |
Language | Haskell2010 |
This module provides an alternative definition for
groupBy
which does not require a transitive
equivalence predicate.
Documentation
groupBy :: (a -> a -> Bool) -> [a] -> [[a]] Source #
Groups adjacent elements according to some relation. The relation can be an equivalence:
>>>
groupBy (==) "aaabcccdda"
["aaa","b","ccc","dd","a"]
>>>
groupBy (==) []
[]
However, it need not be. The function compares adjacent elements only, so it can be used to find increasing subsequences:
>>>
groupBy (<=) [1,2,2,3,1,2,0,4,5,2]
[[1,2,2,3],[1,2],[0,4,5],[2]]
It is fully lazy:
>>>
head (groupBy (==) (1:2:undefined))
[1]
>>>
(head . head) (groupBy undefined (1:undefined))
1
>>>
(head . head . tail) (groupBy (==) (1:2:undefined))
2
xs === concat (groupBy (applyFun2 p) xs)
all (not . null) (groupBy (applyFun2 p) xs)