Samuel Burbano
Coding is easy, logically coding is what makes it special.
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"
};
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 }
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 }
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", "!"]
const thankYou = {
nombre: "ariana",
apellido: "grande",
};
for (let el of thankYou) {
console.log(el);
}
// ERROR: thankYou is not iterable
for (let prop in thankYou) {
console.log(prop);
}
// nombre
// apellido
const a = Symbol();
const b = Symbol();
console.log(a === b); // false
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"
const thankYou = {
nombre: "ariana",
apellido: "grande",
[Symbol.iterator]: function*() {
yield this.nombre;
yield this.apellido;
}
};
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
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
})
}
}
}
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!
}
By Samuel Burbano