Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collection Helpers #16

Open
pemrouz opened this issue Jul 23, 2019 · 0 comments
Open

Collection Helpers #16

pemrouz opened this issue Jul 23, 2019 · 0 comments

Comments

@pemrouz
Copy link
Contributor

pemrouz commented Jul 23, 2019

reduce is the operator that is responsible for serialising new collections, and there's a shorthand generalisation that makes it more ergonomic i.e. reduce([]) to collect into an array, reduce({}) to collect into an object, etc. Currently it knows about a limited set of types, but was also planning to make it a Symbol-based protocol. The prototype just needs one Symbol that dictates how to concat new values it receives into itself.

With Emitter we can tease apart the three concerns of "iteration, transformation, and accumulation". Here's a snippet from one of the tests that explores the entire matrix of going from/to Array, Object, String, Number, Function, Generator, Async Generator, Emitter with a series of transformations in between.

@gsathya, here's what I was thinking re: collection helpers proposal. We can spec it in a generic way to work with all collections rather than duplicating functionality:

Map.prototype.map = function(fn){ return run(this, map(fn), reduce(new this.constructor) ) }
Set.prototype.map = function(fn){ return run(this, map(fn), reduce(new this.constructor) ) }

That is, each of the collection helper methods apply the corresponding transform and serialise the values into a new collection of the same type.

(Note: new this.constructor can also be new this.constructor[Symbol.species], but @syg has more thoughts on that).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant