Generators in JavaScript: A Lazy Zip
In [60]:
function *zip(...[first, ...iterables]){
const length = iterables.length;
let item;
while(true){
item = first.next();
if (item.done) break;
const acc = [];
for(let i = 0; i < length; i++){
const item_ = iterables[i].next();
acc.push(item_.value);
}
yield [item.value, ...acc];
}
}
In [61]:
function *iter(item){
yield* Array.from(item);
}
In [108]:
function *range(start, stop, step=1){
if(step < 1) throw new Error('step must be 1 or greater.');
if(stop <= start) throw new Error('stop must be larger than start');
yield* Array.from({length: ((stop - start) / step)}, (_, i) => start + (i * step));
}
Convenient method to remember where the letter 'A' or 'a' starts in ASCII table.
ASCII uses the last 7 bits of a byte (8 bits): 00000000
Set the first bit to '0' and the second bit to '1'.
Set all except the last bit to 0.
Set the last bit to 1.
01000001
That makes 65.
Set the third bit to 1 and that makes 97.
01100001
I learned this tidbit here:
In [126]:
var [UPPERCASE, LOWERCASE] = [
0b01000001, // 65
0b01100001, // 97
].map(n => String.fromCharCode(...range(n, n+26)))
In [127]:
for (let item of zip(range(0, 26, 1), iter(UPPERCASE), iter(LOWERCASE))){
console.log(item);
}