Skip to content

Instantly share code, notes, and snippets.

@awkward-ninja
Created November 23, 2017 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awkward-ninja/badc2c502c39f29d7e33d5e68de4f5eb to your computer and use it in GitHub Desktop.
Save awkward-ninja/badc2c502c39f29d7e33d5e68de4f5eb to your computer and use it in GitHub Desktop.
Monotonic sequence that is also its current value (in ES6)
'use strict';
class SequencedNumber extends Number {
advance() { this._next = this._current + 1; }
constructor (initial) {
super(initial);
this._current = initial;
this._next = initial;
}
valueOf() { return this._current; }
next() {
this._current = this._next;
this.advance();
return { done: false, value: this._current };
}
[Symbol.iterator]() { return this; }
}
let square = (x) => x * x;
module.exports = Array.from(function* () {
let i = 0, sn = new SequencedNumber(3);
for (let n of sn) {
if (i++ >= 5) break;
yield [square(n), square(sn)];
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment