Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

惰性遍历数组 #2

Open
CarterLi opened this issue Oct 3, 2018 · 1 comment
Open

惰性遍历数组 #2

CarterLi opened this issue Oct 3, 2018 · 1 comment

Comments

@CarterLi
Copy link

CarterLi commented Oct 3, 2018

function* range(start: number, end: number) {
  for (let i = start; i <= end; ++i) yield i;
}
function* map(iter: IterableIterator<number>, fn: (n: number) => number) {
  for (const x of iter) yield fn(x);
}
function* reverse(iter: IterableIterator<number>): IterableIterator<number> {
  for (const x of iter) yield* reverse(iter), yield x;
}
function foreach(iter: IterableIterator<number>, fn: (n: number) => void) {
  for (const x of iter) fn(x);
}

var numbers = range(1, 10);
numbers = map(numbers, function (n) { return n * n });
numbers = reverse(numbers);
foreach(numbers, console.log);
@CarterLi
Copy link
Author

CarterLi commented Oct 3, 2018

image

[手动斜眼]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant