Feature #44
groupByLeader / groupByTail
| Status : | Assigned | Start : | ||
| Priority : | Normal | Due date : | ||
| Assigned to : | John Goerzen | % Done : | 0% |
|
| Category : | - | |||
| Target version : | - | |||
| Resolution : |
Description
Hi.
I find these functions very useful and I alsways get them wrong the first time. Do you think they should go into Data.List?
groupByLeader :: (a -> Bool) -> [a] -> a
groupByLeader p = groupBy (\_ l -> not (p l))
groupByLeader :: (a -> Bool) -> [a] -> a
groupByLast p = groupBy (\l _ -> not (p l))
This is very handy when parsing data with segments that are marked either by a certain head, or a cartain tail, such as the output of pdftk dump_data, or ini files, or debian control files, or ... well, there are plenty.
History
02/22/2007 11:02 AM - John Goerzen
- Status changed from New to Assigned
Could you write up a Haddock comment for these, including examples? If do, I think they would make sense being added to Data.List.Utils.
02/22/2007 12:35 PM - mail-joachim-breitner-de -
Ok, here is my shot at it, although most real-life examples would probably be too large for the documentation.
groupByLeader
Splits the list in a list of sublists, starting a new sublist with every element for which the predicate is true.
Example:
groupByLeader (\n -> n mod 10 0) [2,4,6,8,10,12,14,17,18,20,22,24] -> [[2,3,4,6,8],[10,12,14,17,18],[20,22,24]]
groupByLast
Splits the list in a list of sublists, finishing each sublist with an element for which the predicate is true.
Example:
groupByLeader (\n -> n mod 10 0) [2,4,6,8,10,12,14,17,18,20,22,24] -> [[2,3,4,6,8,10],[12,14,17,18,20],[22,24]]
I can’t think of a more useful one-line-example, sorry.