function iterateOver(...args) {
let [from, to, step=1] = [...args];
const range = {
[Symbol.iterator]() {
this.current = from;
return this;
},
next() {
if (this.current <= to) {
let char = this.current;
this.current = String.fromCharCode(this.current.charCodeAt(0) + step);
return {
done: false,
value: char
};
} else {
return {
done: true
};
}
}
}
return range;
}
for (let num of iterateOver("A","W",5)) {
console.log(num);
}
let t = Array.from(iterateOver("A","J"), c => c.toLowerCase());
console.log(t);
fichier source
fichier source
JS Bin on jsbin.com
Array.from + itérateur
let range = {
from: "A",
to: "W"
};
// 1. call to for..of initially calls this
range[Symbol.iterator] = function() {
// 2. ...it returns the iterator:
return {
current: this.from,
last: this.to,
// 3. next() is called on each iteration by the for..of loop
next() {
// 4. it should return the value as an object {done:.., value :...}
if (this.current <= this.last) {
let char = this.current;
this.current = String.fromCharCode(this.current.charCodeAt(0)+1);
return { done: false, value: char };
} else {
return { done: true };
}
}
};
};
for (let num of range) {
console.log(num);
}
let t = Array.from(range,c=>c.toLowerCase());
console.log(t);
JS Bin on jsbin.com
from: "A",
to: "W"
};
// 1. call to for..of initially calls this
range[Symbol.iterator] = function() {
// 2. ...it returns the iterator:
return {
current: this.from,
last: this.to,
// 3. next() is called on each iteration by the for..of loop
next() {
// 4. it should return the value as an object {done:.., value :...}
if (this.current <= this.last) {
let char = this.current;
this.current = String.fromCharCode(this.current.charCodeAt(0)+1);
return { done: false, value: char };
} else {
return { done: true };
}
}
};
};
for (let num of range) {
console.log(num);
}
let t = Array.from(range,c=>c.toLowerCase());
console.log(t);
code
JS Bin on jsbin.com
Array.from : magic
t = Array.from(Fib)
console.log(t);
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
fichier code
code
[Symbol.iterator]() {
let n1 = 1,
n2 = 1;
return {
[Symbol.iterator]() {
return this;
},
next() {
let current = n2;
n2 = n1;
n1 = n1 + current;
if (current <= 1000) { //sinon la boucle est infinie
return {
value: current,
done: false
};
} else {
return {
done: true
};
}
return {
value: current,
done: false
};
},
return () {
console.log(
`stop`
);
return {
done: true
};
}
};
}
};
t = Array.from(Fib)
console.log(t);
console.log(t);
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
fichier code
code
En action :
let Fib = {[Symbol.iterator]() {
let n1 = 1,
n2 = 1;
return {
[Symbol.iterator]() {
return this;
},
next() {
let current = n2;
n2 = n1;
n1 = n1 + current;
if (current <= 1000) { //sinon la boucle est infinie
return {
value: current,
done: false
};
} else {
return {
done: true
};
}
return {
value: current,
done: false
};
},
return () {
console.log(
`stop`
);
return {
done: true
};
}
};
}
};
t = Array.from(Fib)
console.log(t);
itérator :
Voici deux écritures de Fibonacci :
const Fibonacci = {
n1: 1,
n2: 1,
[Symbol.iterator]() {
return this;
},
next() {
let current = this.n2;
this.n2 = this.n1;
this.n1 = this.n1 + current;
return {
value: current,
done: false
};
},
return (v) {
return {
value: v,
done: true
};
}
};
let Fib = {
[Symbol.iterator]() {
let n1 = 1,
n2 = 1;
return {
[Symbol.iterator]() {
return this;
},
next() {
let current = n2;
n2 = n1;
n1 = n1 + current;
return {
value: current,
done: false
};
},
return () {
console.log(
`stop`
);
return {
value: v,
done: true
};
}
};
}
};
for (var v of Fib) {
console.log(v);
if (v > 50) break;
}
const Fibonacci = {
n1: 1,
n2: 1,
[Symbol.iterator]() {
return this;
},
next() {
let current = this.n2;
this.n2 = this.n1;
this.n1 = this.n1 + current;
return {
value: current,
done: false
};
},
return (v) {
return {
value: v,
done: true
};
}
};
let Fib = {
[Symbol.iterator]() {
let n1 = 1,
n2 = 1;
return {
[Symbol.iterator]() {
return this;
},
next() {
let current = n2;
n2 = n1;
n1 = n1 + current;
return {
value: current,
done: false
};
},
return () {
console.log(
`stop`
);
return {
value: v,
done: true
};
}
};
}
};
for (var v of Fib) {
console.log(v);
if (v > 50) break;
}
itérateur
let range = {
from: 65,
to: 90,
[Symbol.iterator]() {
return this;
},
next() {
if (this.from <= this.to) {
return {
done: false,
value: String.fromCharCode(this.from++)
};
} else {
return {
done: true
};
}
},
};
// A-W
for (let num of range) {
console.log(num);
}
from: 65,
to: 90,
[Symbol.iterator]() {
return this;
},
next() {
if (this.from <= this.to) {
return {
done: false,
value: String.fromCharCode(this.from++)
};
} else {
return {
done: true
};
}
},
};
// A-W
for (let num of range) {
console.log(num);
}
Inscription à :
Articles (Atom)