Opened 10 years ago

Closed 10 years ago

Last modified 7 years ago

#1408 closed feature request (wontfix)

groupWhen – a groupBy that compares consecutive values

Reported by: Joachim Breitner <mail@…> Owned by:
Priority: normal Milestone:
Component: libraries/base Version: 7.0.2
Keywords: Cc: ghc@…, rturk@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

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.

Example code:

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

Uses:

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)

or

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

Change History (11)

comment:1 Changed 10 years ago by Joachim Breitner <mail@…>

More comments and other implementations on http://hpaste.org/141

comment:2 Changed 10 years ago by ross

Component: libraries (other)libraries/base

The Haskell 98 Report (s17.6) says "When the By function replaces an Eq context by a binary predicate, the predicate is assumed to define an equivalence". Since the new function agrees with groupBy in that case, I think it would be reasonable to redefine groupBy as this more useful version.

The same applies to nubBy, and possibly deleteBy, deleteFirstsBy and intersectBy (which could have more general types to make this clear).

On the downside, it would mess up the simple uniform treatment of the By functions.

comment:3 Changed 10 years ago by igloo

Resolution: wontfix
Status: newclosed

I think that if we have both groupBy and this new function then the names should be more similar (I'd expect groupWhen to do something completely different), and if possible allude to the functionality difference.

Are there any cases when the existing behaviour is better?

No matter what, I think this should go via http://www.haskell.org/haskellwiki/Library_submissions so I'm closing this bug.

By the way, the hpaste link seems to now be about catching monkeys.

Thanks

Ian

comment:4 Changed 10 years ago by mail@…

I’m currently not interested enough to follow the complete procedure. Can’t you just reassign the bug somewhere appropriate, until someone picks it up?

comment:5 Changed 9 years ago by simonmar

Architecture: UnknownUnknown/Multiple

comment:6 Changed 9 years ago by simonmar

Operating System: UnknownUnknown/Multiple

comment:7 Changed 8 years ago by simonmar

difficulty: Easy (1 hr)Easy (less than 1 hour)

comment:8 Changed 7 years ago by Lemming

Cc: ghc@… added
Type of failure: None/Unknown

comment:10 Changed 7 years ago by Remi

Cc: rturk@… added

comment:11 Changed 7 years ago by maeder

I'ld like to add http://www.haskell.org/pipermail/libraries/2009-November/012794.html where such a function is called "runs".

Note: See TracTickets for help on using tickets.