ES2015 Generators で遊んでみた

ES2015のGeneratorsで階乗を算出するコードを書いてみた.

ES2015のコード (Chromeであればそのまま動作します)

let factorial = {
  [Symbol.iterator]: function*(max) {
    let current = 1;

    for (let counter = 1; counter <= max; counter++) {
      current *= counter;
      yield current;
    }
  }
};

let f = factorial[Symbol.iterator](5);

for (let e = f.next(); !e.done; e = f.next()) {
    console.log(e.value);
} 

ES2015 Iterators で遊んでみた

ES2015のIteratorsで階乗を算出するコードを書いてみた.

まずは, ES2015のコード (Babel)

let factorial = {
  [Symbol.iterator](max) {
    let current = 1;
    let counter = 1;

    return {
      next() {
        current *= counter;
  
        if (counter > max) {
          return {done: true, value: current}
        } else {
          counter++;

          return {done: false, value : current}
        }
      }
    }
  }
};

let f = factorial[Symbol.iterator](5);

for (let e = f.next(); !e.done; e = f.next()) {
  console.log(e.value);
}

ES5で出力されたコード

"use strict";

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var factorial = _defineProperty({}, Symbol.iterator, function (max) {
  var current = 1;
  var counter = 1;

  return {
    next: function next() {
      current *= counter;

      if (counter > max) {
        return { done: true, value: current };
      } else {
        counter++;

        return { done: false, value: current };
      }
    }
  };
});

var f = factorial[Symbol.iterator](5);

for (var e = f.next(); !e.done; e = f.next()) {
  console.log(e.value);
}

for..of を使って無限に階乗を出す場合 (当然打ち切る処理は入る).

ES2015

let factorial = {
  [Symbol.iterator]() {
    let current = 1;
    let counter = 1;

    return {
      next() {
        current *= counter;
        counter++;

        return {done: false, value : current}
      }
    }
  }
};

for (let e of factorial) {
    if (e > 10000) {
        break;
    }

    console.log(e);
}

ES5

"use strict";

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var factorial = _defineProperty({}, Symbol.iterator, function () {
  var current = 1;
  var counter = 1;

  return {
    next: function next() {
      current *= counter;
      counter++;

      return { done: false, value: current };
    }
  };
});

var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;

try {
  for (var _iterator = factorial[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
    var e = _step.value;

    if (e > 10000) {
      break;
    }

    console.log(e);
  }
} catch (err) {
  _didIteratorError = true;
  _iteratorError = err;
} finally {
  try {
    if (!_iteratorNormalCompletion && _iterator.return) {
      _iterator.return();
    }
  } finally {
    if (_didIteratorError) {
      throw _iteratorError;
    }
  }
}