Wintus
9/16/2017 - 11:19 PM

es6-toybox

ES6 generators

  • Fibonacci
  • FizzBuzz
const iota = (n = 0) => Array(n).fill().map((_, i) => i);
function* fibGen(first = 0, second = 1) {
  let [now, next] = [first, second];
  while (true) {
    yield now;
    [now, next] = [next, now + next];
  }
}

function* fizzBuzzGen(num = 0) {
  let n = num;

  const fizz = [3, "Fizz"];
  const buzz = [5, "Buzz"];
  const pairs = [fizz, buzz];

  while (true) {
    const str = pairs.map(pair => !(n % pair[0]) ? pair[1] : '').join('');
    yield str || n;
    ++n;
  }
}