tc39 / proposal-collection-methods

https://tc39.github.io/proposal-collection-methods/
Other
172 stars 8 forks source link

What exactly is the use case for `Set.prototype.map` returning a set? #33

Closed dead-claudia closed 5 years ago

dead-claudia commented 5 years ago

I'm having a tough time figuring out what a set.map(func) -> set is for. I could see value in a set.map(func) -> iterator inherited from an %IterablePrototype% as it makes no sense to special case it there, but I'm not sure how useful a standalone, deduplicating set.map(func) -> set would be.

zloirock commented 5 years ago

I need set.map(func) -> set too often and I don't remember any use case of set.map(func) -> iterator. If you need it, use set.values().map(func) -> iterator from this proposal.

ljharb commented 5 years ago

Virtually every use case for mapping over an array applies to a Set, and vice versa.

If you’re asking about whether it should return an iterator instead of reifying a new Set, that question applies to every method in this proposal, and the answer is “methods on X typically produce a new X”. The iterator helpers proposal seems like it’d be what you want.

dead-claudia commented 5 years ago

@ljharb @zloirock

I'm familiar with the iterator helpers proposal, and that's precisely why I was asking what the use case for set.map was. I've not encountered a single scenario, either in my own code or anyone else's I've read, where this was actually useful.

[@ljharb:] Virtually every use case for mapping over an array applies to a Set, and vice versa.

I can see this in the abstract, but what does this mean concretely?

[@zloirock:] I need set.map(func) -> set too often

Could you give me examples of when you need this?

zloirock commented 5 years ago

For example, I need to do something with a collection (for example, extract property from an object), store the result and use .has method with O(1) complexity on it. At least 3 cases for 2 previous days.

ljharb commented 5 years ago
dead-claudia commented 5 years ago

So the new Set(set.values().map(...)) (borrowing from the iterator helpers) is common enough to warrant inclusion? If so, I'll go ahead and close this issue.

ljharb commented 5 years ago

Even if we had iterator helpers first, imo we'd still want .map on arrays - the existence of a generic solution doesn't preclude the usefulness of a specific one.