Skip to content

Instantly share code, notes, and snippets.

@raibima
Created January 21, 2020 08:58
Show Gist options
  • Select an option

  • Save raibima/39296a93bb49444a30772d8381b5b7e5 to your computer and use it in GitHub Desktop.

Select an option

Save raibima/39296a93bb49444a30772d8381b5b7e5 to your computer and use it in GitHub Desktop.
Generator Example 1
import { range, map } from './utils.js';
let iter = range(0, 1000); // almost-zero memory & computation overhead!
let list = (
<ul>
{map(iter, n => <li key={n}>{n}</li>)
</ul>
);
export function range(start, end) {
for (let i = start; i < end; i++) {
yield i;
}
}
export function map(iterable, mapper) {
let result = [];
let index = 0;
for (let value of iterable) {
result.push(mapper(value, index++));
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment