groupWhen – a groupBy that compares consecutive values
|Reported by:||Owned by:|
|Type of failure:||None/Unknown||Test Case:|
|Related Tickets:||Differential Rev(s):|
groupBy has a minor problem: It always uses the first value of a group to decide whether a new value belongs to this group or the next. In several cases it would be more useful if it would take the last value of a group, thus always comparing consecutive values.
groupWhen :: (a -> a -> Bool) -> [a] -> [[a]] groupWhen _  =  groupWhen _ [a] = [[a]] groupWhen f (a:l) = if f a (head c) then (a:c):r else [a]:c:r where (c:r) = groupWhen f l
groupWhen (\a b -> b - a < 5) [1,2,4,10,14,16,18] -- Finding holes in a increasing series, e.g. log time stamps (my real use case)
groupWhen (<) [1,2,3,2,10,12,10,11] -- Group into strictly increasing sublists
Note that for transitive and symetrical comparision functions
f (such as
groupBy f == groupWhen f.
It should probably go to Data.List