JS

Iterators

function nombreDelIterador() {
  return {
    next: function() {
      return {
        value: "El valor actual del iterador",
        done: false, // true | false
      }
    }
  };
}
// Samuel Burbano
const socials = {
  twitter: "@iosamuel",
  facebook: "iosamuel.dev",
  github: "iosamuel",
  web: "iosamuel.dev"
};

JS

Iterators

function crearIterador(arreglo) {
  var siguienteIndice = 0;
  
  return {
    next: function() {
        return siguienteIndice < arreglo.length
          ? { value: arreglo[siguienteIndice++], done: false }
          : { done: true };
    }
  }
}

const thankYou = crearIterador(['ariana', 'grande']);
console.log(thankYou.next()); // { value: "ariana", done: false }
console.log(thankYou.next()); // { value: "grande", done: false }
console.log(thankYou.next()); // { value: undefined, done: true }

JS

Generators

function* crearGenerator(arreglo) {
  let index = 0;

  while (index < arreglo.length) {
    yield arreglo[index++];
  }
}

const thankYou = crearGenerator(['ariana', 'grande']);
console.log(thankYou.next()); // { value: "ariana", done: false }
console.log(thankYou.next()); // { value: "grande", done: false }
console.log(thankYou.next()); // { value: undefined, done: true }

JS

Iterables

Array()

Map()

Set()

Symbol.iterator

String()

JS

Iterables

for..of

[...iterable]

const miIterable = [1, 2, 3, 4, 5];
for (let valor of miIterable) {
    console.log(valor)
}
// 1
// 2
// 3
// 4
// 5
const miIterable = "Hola!";
[...miIterable] 
// ["H", "o", "l", "a", "!"]

for..of

for..in

!==

JS

Iterables

const thankYou = {
  nombre: "ariana",
  apellido: "grande",
};
for (let el of thankYou) {
  console.log(el);
}
// ERROR: thankYou is not iterable

for..of

for..in

!==

for (let prop in thankYou) {
  console.log(prop);
}
// nombre
// apellido

JS

Symbol

Symbol()

Symbol()

!=

const a = Symbol();
const b = Symbol();

console.log(a === b); // false

JS

Symbol

const a = Symbol();
const b = Symbol();

const obj = {};
obj[a] = "a";
obj[b] = "b";
obj.c = "c";
obj.d = "d";

for (let i in obj) {
  console.log(i); // solo imprime "c" y "d"
}

obj[a]; // "a"

JS

Symbol.iterator

const thankYou = {
  nombre: "ariana",
  apellido: "grande",
  [Symbol.iterator]: function*() {
    yield this.nombre;
    yield this.apellido;
  }
};

JS

const arreglo = [...thankYou]; // ["ariana", "grande"]

for (let valor of thankYou) {
  console.log(valor);
}
// ariana
// grande

const [ nombre, apellido ] = thankYou;
console.log(nombre, apellido); // ariana grande

Symbol.iterator

JS

Ejemplo

class RandomArray extends Array {
  [Symbol.iterator]() {
    let getRange = n => Array.from(new Array(n), (_, i) => i); // [0, n)
    let shuffle = arr => { // Durstenfeld shuffle
      for (let i = arr.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [arr[i], arr[j]] = [arr[j], arr[i]];
      }
      return arr;
    };

    let i = 0;
    let indices = shuffle(getRange(this.length));

    return {
      next: () => ({
        value: this[indices[i++]],
        done: i > this.length
      })
    }
  }
}

JS

let canciones = new RandomArray("7 rings", "Bang Bang", "Thank U, Next", "Side to Side", "break up with your girlfriend, i'm bored", "Into You", "Problem", "god is a woman", "No Tears Left to Cry", "Don’t Call Me Angel", "dangerous woman", "Boyfriend", "One Last Time", "Breathin", "Love Me Harder", "Santa Tell Me", "Almost Is Never Enough", "Focus", "Break Free", "needy");
console.log([...canciones]);
console.log([...canciones]);
console.log([...canciones]);

for (let cancion of canciones) {
  console.log(cancion); // CANCION RANDOM!
}

Ejemplo

JS

Ejemplo

https://github.com/iosamuel/RunoX

Iterators

By Samuel Burbano

Iterators

  • 305