I’ve noticed that group
and groupOn
behave rather unintuitively:
let list = [ "A", "B", "A", "A", "C" ]
let grouped = group list
debug grouped
returns:
[["A"],["B"],["A","A"],["C"]]
The docs say the following:
The ‘group’ function groups equal elements into sublists such that the concatenation of the result is equal to the argument.
So the docs do say - albeit in a bit of an odd way - that only successive equal elements will be grouped. Intuitively, I would’ve expected for any equal elements in the list to be grouped (but then the statement about concatenation returning the original argument doesn’t hold anymore). Similarly, I would expect the groupOn
function to group all elements that return the same key, regardless of where they occur in the list. Furthermore, I’d expect it to return tuples that contain the keys as well as the elements, because usually I’d want to interate over the keys to process the groups.
I’m wondering what the rationale behind the current implementation is, I can’t think of a use case from the top of my head where I’d use it the way it’s implemented right now. Reason I’m asking this is that I’ve just spent far too long debugging an issue that came down to that confusion, and I’m guessing I might not be the only one running into it.