I am writing the following function the second time in unrelated projects (first was XML processing, now custom command line flag processing), and I have a feeling that it should exist in some library, I just cannot find it. It groups list elements, each group starting at an element where a predicate is true.
Any simpler ways to do this?
groupStartBy :: (a -> Bool) -> [a] -> [[a]]
groupStartBy pred xs = reverse $ map reverse $ foldl' step [] xs
where
step as x | pred x = [x]:as
step (a:as) x = (x:a):as
step [] x = [[x]]
You can use
groupByto do this: