-
Notifications
You must be signed in to change notification settings - Fork 17
groupByMap naming #30
Comments
|
I do understand how it works. I'm saying that someone unfamiliar with the spec who is familiar with |
We're changing from |
@jridgewell what means |
The way it was phrased during discussion was that we're converting "groupings" to a Map, not the object return value from |
In this case, it's unrelated to |
|
@zloirock I agree it's awkward, but when "group by" is followed by a noun, it usually means "group by {noun}" literally - "group by name", "group by size", etc. - so group-by-map reads as "group by map" rather than "group by" + "(map variant of this method)" absent prior context. |
@bathos so maybe that means that
|
I’m pretty confident grouping and producing a Map will be a fairly uncommon use case; it’s just an important one in those rare cases. The “by” was considered critical to keep by multiple delegates, because “group by” has an existing connotation about what the callback returns. |
I'm just one developer working in one team and can't say how common or uncommon this is, but as a little piece of anecdotal evidence, I'll mention that we already use a Map-producing The even/odd example is one where the object-producing version shines because the produced keys are well known and simple. But for me, grouping by (semi-)arbitrary user inputted data is a much more compelling and common use case, and there Maps are quite useful. I don't have strong opinions about the naming, though, just wanted to put that out there. |
I have cases for a such method several times for a month.
We can say almost the same about most of |
because neither of those return a key name, which is what the "by" strongly implies. |
I agree this will probably be common, which is why I asked for it. I don't know why @ljharb imagines otherwise. As to the names: (OK, sometimes |
@ljharb @bakkot those names, even |
|
Not a true subset, but it seems fair to say filter is a kind of "grouping": However filter seems like an argument for keeping "by" because it's not exactly intuitive that you return true to say don't filter. |
@bathos that you can do every array method with |
The name of the operation is still "groupBy". There is the default one and there is the one which goes to a Map. Hence "groupBy" and "groupByToMap". This seems fine to me. Removing the "by" would be confusing because it is part of the name of the operation. |
Ok, I will not impose my opinion further if others are against it. @ljharb |
I think what's notable in this case is that you can get a result that includes the filter result with the same callback you would pass to filter itself @ljharb, which is not like everything-can-be-reduce, but I agree it's not particularly significant either way. |
I know that this issue was closed as if there's nothing left to discuss and everybody else's opinion is unimportant, but |
The name of the operation is "groupBy"; I think any name which doesn't keep that as a substring is not a good choice. |
I agree with the I think further bikesheding the name is unlikely to produce a better phrase, and I don't think the new name is so terrible that we should delay implementations (the proposal is now Stage 3, the implementation stage). |
FWIW I would have called it I don't expect these names to be used. But this is a bikeshedding thread so I'm just dropping them here so they can be said to have been considered and rejected. I actually think @fisker is onto something and Javascript has a problem about how and where type conversion happens. But that's off topic. These are useful functions, whatever they end up being called. *wanders into the sunset muttering something about quotients and equivalence classes...* |
Out of curiosity, was there any consideration for making this particular method a factory method on the Map class? I imagine something like Perhaps it is less discoverable not as an array method, but if this functionality is only intended to meet a few rare use cases, discoverability doesn't seem that important. This could also allow it to work with any iterable (object supporting the |
Not sure if I even like this, but throwing this in here that there could be arr.groupBy(groupFunc, Map) |
I don't think there is consensus on that aspect: for some, use might be rare and for others, use might be common. I don't think discoverability would be significantly reduced if the returns-a-Map variant were associated with the
I would expect something like If the |
That's a fair point. I thought the index could still be useful in some situations and easily determined from storing and incrementing a value during iteration, but you're right that it's associated more with ordered data. Either way, it's not the most inconvenient thing to spread the iterable data to an array first and then pass it to the function. In hindsight, maybe that's preferable because it enforces the input type to be the same as (array-like) the type the elements are grouped into.
As mentioned above, I think providing the index of the element just indicates that the function should act on an array somehow. However, it feels a bit reductionist to conclude that it must then belong on the My concern is that it feels a bit strange to have On the other hand, with a factory method on the |
That kind of interaction between different collection types is normal in the standard libraries of other languages; for example, Of course, that doesn't mean the returns-a-Map variant must then belong on the |
flatMap
means ".flat after .map". It kind of seems likegroupByMap
should mean ".groupBy after .map", by that precedent.I'm not sure I have a better naming suggestion, but perhaps it's worth bikeshedding?
The text was updated successfully, but these errors were encountered: