Created
January 21, 2020 08:58
-
-
Save raibima/39296a93bb49444a30772d8381b5b7e5 to your computer and use it in GitHub Desktop.
Generator Example 1
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
| 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> | |
| ); |
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
| 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