Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
Created January 3, 2016 20:33
Show Gist options
  • Save vlad-bezden/8f13051f8bea9dc56a86 to your computer and use it in GitHub Desktop.
Save vlad-bezden/8f13051f8bea9dc56a86 to your computer and use it in GitHub Desktop.
Range Function

Range Function

Example of custom implementation 'range' function using generator and ES2015 syntax.

A Pen by Vlad Bezden on CodePen.

License.

'use strict';
let range = function*(start, end) {
let current = start;
while (current <= end) {
let delta = yield current;
current += delta || 1;
}
};
let data = range(1, 10);
console.log(Array.from(data));
let data2 = [];
let iterator = range(1, 10);
let next = iterator.next();
while (!next.done) {
data2.push(next.value);
next = iterator.next(2);
}
console.log(data2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment