Created
February 7, 2021 16:37
-
-
Save Sc4ramouche/313441a65d6aba58fa2160a3dbafe553 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
Simple iterator implementation that allows to calculate sequenced values. | |
Next value is calculated by the `update` function. | |
This construction might come in handy when it's required to generate a sequence, | |
but there's also no need to keep the history of previous values. | |
Note: could also be elegangtly descibed via class syntax with private fields. | |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields | |
*/ | |
function Iterator(update = x => x, initialValue = 0) { | |
this.value = initialValue; | |
this.next = () => this.value = update(this.value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment