Would ES6 have changed Underscore externally?
(I think so.)
var activeUsers = _.filter(users, function(user) { return user.isActive }); var ages = _.map(activeUsers, function(user) { return user.age }); var maxAge = _.reduce(ages, max, 0);
var activeUsers = _.filter(users, function(user) { return user.isActive }); var ages = _.map(activeUsers, function(user) { return user.age }); var maxAge = _.reduce(ages, max, 0);
function* map(items, transform) { for (item of items) yield transform(item); }
Notice how similiar this is to the previous code.
function* map(items, transform) { for (item of items) yield transform(item); } function* filter(items, predicate) { for (item of items) if (predicate(item)) yield item; }
Notice how similiar this is to the previous code.
function* map(items, transform) { for (item of items) yield transform(item); } function* filter(items, predicate) { for (item of items) if (predicate(item)) yield item; } var activeUsers = filter(users, function(user) { return user.isActive }); var ages = map(activeUsers, function(user) { return user.age }); var maxAge = reduce(ages, max, 0);
Notice how similiar this is to the previous code.
var iterable = {}; iterable[iterator] = function() { /* ... */ };
var iterable = {}; iterable[iterator] = function() { /* ... */ }; var iter8tor = iterable[iterator]();
var iterable = {}; iterable[iterator] = function() { /* ... */ }; var iter8tor = iterable[iterator](); iter8tor.next() // -> {value: 'item 1', done: false}
var iterable = {}; iterable[iterator] = function() { /* ... */ }; var iter8tor = iterable[iterator](); iter8tor.next() // -> {value: 'item 1', done: false} iter8tor.next() // -> {value: 'item 2', done: false}
var iterable = {}; iterable[iterator] = function() { /* ... */ }; var iter8tor = iterable[iterator](); iter8tor.next() // -> {value: 'item 1', done: false} iter8tor.next() // -> {value: 'item 2', done: false} iter8tor.next() // -> {value: 'item 3', done: false}
var iterable = {}; iterable[iterator] = function() { /* ... */ }; var iter8tor = iterable[iterator](); iter8tor.next() // -> {value: 'item 1', done: false} iter8tor.next() // -> {value: 'item 2', done: false} iter8tor.next() // -> {value: 'item 3', done: false} iter8tor.next() // -> {value: undefined, done: true}
Generators combine with iterators to allow us to extend the built-in for…of syntax in powerful ways:
jQuery.prototype[iterator] = function* iterator() { for (var i = 0; i < this.length; i++) yield $(this.get(i)); } for (var p of $('p')) p.fadeOut();
From @littlecalculist's wiki.
These slides are online at: aaronj1335 / if-underscore-was-written-in-es6