Set some, every, reduce, filter, map methods

Herby Vojčík herby at mailbox.sk
Sat Mar 30 09:25:29 PDT 2013


Well, why not, but forEach, some, every, reduce[Right] and filter are very natural operations useful for _any_ collection (map is harder). If Set has forEach, it should have these as well. Of course, iterators could have them as well.

It is strange if Array and iterators would have it, but other collections would not. The code that takes a collection as a parameter and calls some, every, filter etc. on it would break, instead of doing the job on Set (at least some, every and reduce family can be transparently used; and I would argue for filter as well - it returns subcollection that understands the collection protocol (@@iterator, values, forEach, some, every, filter, reduce, reduceRight)).

Herby

P.S.: What should generic collection contract consist of in ES6? I would say the except those named above only needed addition is isEmpty, length/size, and possibly clear as well as copy/clone (slice is not right for Set/Map).

Erik Arvidsson wrote:
> I said this in the other thre
ad but I don't mind repeating myself. A
> better way forward is to provide these for iterators.
>
> mySet.values().some(func)
> mySet.values().every(func)
> mySet.values().reduce(func)
> new Set(mySet.values().filter(func))
> new Set(mySet.values().map(func))
>
> I'm not opposed to adding these to Set and Map but I think it is more
> important to define these using iterators first since it scales better
> to new collection types.



More information about the es-discuss mailing list