Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A, B, C. 1, 2, 3. Iterables you and me.

A, B, C. 1, 2, 3. Iterables you and me.

The Iterable protocol was introduced in 2015, but it wasn't really caught on, and people have doubts regarding how it works, how can we leverage it to write better and more expressive code. This talk tries to break this fantastic ECMAScript feature down in a one-two step, showing little by little the use cases, properties, and the *new async Iterator protocol*, quickly and smoothly, like trying to learn how to dance this fun Jackson's 5 Soul music ;) If you are a beginner in JS, you will learn how to build custom iterable objects in a bunch of different ways, and if you already got it, I will challenge you to go an extra mile and experiment neat tricks like composing iterables or creating a PoC of a state/side effect management based on Iterables.

Willian Martins

May 30, 2019
Tweet

More Decks by Willian Martins

Other Decks in Technology

Transcript

  1. let collection = [1,2,3,4,5,6]; let sum = 0; for (let

    i = 0; i < collection.length; i++) { sum = sum + collection[i]; } console.log(sum); // 21 @wmsbill
  2. let collection = [1,2,3,4,5,6]; let sum = 0, i =

    0; while (i < collection.length) { sum = sum + collection[i]; i++; } console.log(sum); // 21 @wmsbill
  3. let collection = [1,2,3,4,5,6]; let sum = 0, i =

    0; do { sum = sum + collection[i]; i++; } while (i < collection.length); console.log(sum); // 21 @wmsbill
  4. let collection = [1,2,3,4,5,6]; let sum = 0; for (let

    i in collection) { sum = sum + collection[i]; } console.log(sum); // 21 @wmsbill
  5. Array.prototype.foo = “bar”; let collection = [1,2,3,4,5,6]; let sum =

    0; for (let i in collection) { sum = sum + i; } console.log(sum); // 21bar @wmsbill
  6. Array.prototype.foo = “bar”; let collection = [1,2,3,4,5,6]; let sum =

    0; for (let i in collection) { if(collection.hasOwnProperty(i)) { sum = sum + collection[i]; } } console.log(sum); // 21 @wmsbill
  7. let collection = [1,2,3,4,5,6]; let sum = 0; collection.forEach((num) =>

    sum = sum + num); console.log(sum); // 21 @wmsbill
  8. { "23": {id: "23", name: "soccer ball", "value": 30}, "42":

    {id: "42", name: "sneakers", "value": 50}, "12": {id: "12", name: "belt", "value": 10}, "30": {id: "30", name: "baseball bat", "value": 90}, "74": {id: "74", name: "sunglasses", "value": 25}, "92": {id: "92", name: "cap", "value": 40} } @wmsbill
  9. In object-oriented programming, the iterator pattern is a design pattern

    in which an iterator is used to traverse a container and access the container's elements. The iterator pattern decouples algorithms from containers; GoF Design Patterns Book
  10. In object-oriented programming, the iterator pattern is a design pattern

    in which an iterator is used to traverse a container and access the container's elements. The iterator pattern decouples algorithms from containers; Wikipedia
  11. function range (min, max) { return { next () {

    return { value: min++, done: min > max, } } } } @wmsbill Iterator Result Iterator
  12. const myRange = range(10, 20); let next = myRange.next(); while

    (!next.done) { console.log(next.value); next = myRange.next(); } @wmsbill
  13. function range (min, max) { return { [Symbol.iterator] () {

    return { next () { return { value: min++, done: min > max, } } } } } } @wmsbill Iterator Result Iterator Iterable
  14. function range (min, max) { return { [Symbol.iterator] () {

    return { next () { return { value: min++, done: min > max, } } } } } } @wmsbill
  15. function range (min, max) { return { *[Symbol.iterator] () {

    while (min < max) { yield min++; } } } } @wmsbill
  16. const map = (iterable, func) => function* () { for

    (item of iterable) { yield func(item); } }(); @wmsbill
  17. const filter = (iterable, func) => function* () { for

    (item of iterable) { if (func(item)) { yield item; } } }(); @wmsbill
  18. const take = (iterable, num) => function* () { let

    count = 0; for (item of iterable) { if (count++ === num) { return; } yield item; } }(); @wmsbill
  19. pipe( filter(range(10, 100), isMultipleOfFour), result => map(result, double), result =>

    take(result, 5), sink ); // [ 24, 32, 40, 48, 56 ] @wmsbill
  20. pipe( filter(range(10, 100), isMultipleOfFour), result => map(result, double), result =>

    take(result, 5), sink ); [...range(10, 100)] .filter(isMultipleOfFour) .map(double) .slice(0, 5)
  21. interface AsyncIterable { [Symbol.asyncIterator]() : AsyncIterator; } interface AsyncIterator {

    next() : Promise<IteratorResult>; } interface IteratorResult { value: any; done: boolean; } @wmsbill