leebyron / ecmascript-iterator-hof Goto Github PK
View Code? Open in Web Editor NEWHigher Order Functions on Iterators
Higher Order Functions on Iterators
The new for ... of syntax ignores the completion value, so it would make sense to ignore it for the different functions introduced here.
function *example(){
yield 1;
yield 2;
return 3;
}
for(let i of example()) console.log(i);
Outputs
1
2
On can achieve the exact same effect by chaining map
and flatten
. When xstream (an Observable implementation) was being made, the creator, @staltz, omitted "flatMap" because [it was easier to understand]. I think this reasoning applies to higher order functions with Iterators.
Consider this snippet in the readme which incorrectly suggests the return value of flatten
is an array.
var deepValues = [ [ 'A' ], [[ 'B' ]], 'C' ]
var flat = deepValues.values().flatten() // [ 'A', ['B'], 'C' ]
Adding the method collect
:
class Iterator<T> {
collect<R>(fn: (iterator: Iterator<T>) => R): R;
}
Would allow:
var deepValues = [ [ 'A' ], [[ 'B' ]], 'C' ]
var flat = deepValues.values().flatten().collect(Array.from) // [ 'A', ['B'], 'C' ]
Once you introduce a series of operators, people are bound to want to have more.
I think it would be a good idea to introduce a .bind(fn, ...args)
operator which would allow chaining with custom operators without having to subclass or modify the prototype.
There were several discussions about something like that from pre-es6 strawmans, but it's still missed in https://github.com/tc39/proposals
It's not immediately clear to me whether this is the case.
Add Iterator[@@species]
accessor property and IteratorSpeciesCreate
abstract operation so that:
class MyIterator extends Iterator {}
new MyIterator().map(x => x) instanceof MyIterator // true
See:
Would it make sense for this proposal to also define the same functions for Async Iterables to kill two birds with one stone?
Having higher order functions on async iterables would make async programming extremely powerful.
Is this an opportunity to spec the functions as generators? A few examples:
class Iterator {
*concat(...iterators) {
yield* this;
for (iterator in iterators) {
yield* iterator;
}
}
*map(mapFn) {
for (value in this) {
yield mapFn(value);
}
}
*filter(filterFn) {
for (value in this) {
if (filterFn(value)) {
yield value;
}
}
}
*flatten() {
for (iterator in this) {
yield* iterator;
}
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.