IterTools.jl icon indicating copy to clipboard operation
IterTools.jl copied to clipboard

IterTools.groupby doesn't group nonconsecutive elements together.

Open dafnevk opened this issue 5 months ago • 1 comments

When I execute the following code:

my_list = [ ["u",1], ["u",2], ["a",1], ["d", 1], ["a",2],["d",2]]
collect(IterTools.groupby(s->s[1], my_list))

I get as output:

 [["u", 1], ["u", 2]]
 [["a", 1]]
 [["d", 1]]
 [["a", 2]]
 [["d", 2]]

So it seems that the items with key a and d are not properly grouped together.

Environment: Julia v1.10.9 itertools v1.10.0

dafnevk avatar Jul 24 '25 08:07 dafnevk

This is documented.

Group consecutive values that share the same result of applying f.

https://github.com/JuliaCollections/IterTools.jl/blob/5eeba86ecb167993cec41d6dfde171946d398b98/src/IterTools.jl#L385

Same behavour as python's I know some other language/library does it the way you want, but i can't remember which. I feel like i did a writeup at some point. SplitApplyCombine.jl's group is at least on such.

I feel like both are valuable. and having to call sort before groupby seems unnesc.

Maybe we should just have groupby_consective and groupby_nonconsective, and be really explicit.

oxinabox avatar Aug 25 '25 03:08 oxinabox