Created
November 23, 2017 18:49
-
-
Save awkward-ninja/badc2c502c39f29d7e33d5e68de4f5eb to your computer and use it in GitHub Desktop.
Monotonic sequence that is also its current value (in ES6)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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