Skip to content

Instantly share code, notes, and snippets.

@Sc4ramouche
Created February 7, 2021 16:37
Show Gist options
  • Save Sc4ramouche/313441a65d6aba58fa2160a3dbafe553 to your computer and use it in GitHub Desktop.
Save Sc4ramouche/313441a65d6aba58fa2160a3dbafe553 to your computer and use it in GitHub Desktop.
/*
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