Skip to content

Instantly share code, notes, and snippets.

@raganwald
Last active August 29, 2015 14:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raganwald/967f99d90738896cfa06 to your computer and use it in GitHub Desktop.
Save raganwald/967f99d90738896cfa06 to your computer and use it in GitHub Desktop.
ES-6 Gotchas
const arrayIterator = (array) => {
let i = 0;
return () => {
const done = i < array.length;
return {
done,
value: done ? undefined : array[i++]
}
}
}
const brokenIteratorSum = (iterator) => {
let done, value,
sum = 0;;
while (({done, value} = iterator(), !done)) {
sum += value
}
return sum
}
brokenIteratorSum(arrayIterator([1, 4, 9, 16, 25]))
//=> repl: cannot add a declaration here in node type WhileStatement
const iteratorSum = (iterator) => {
let i,
sum = 0;;
while ((i = iterator(), !i.done)) {
sum += i.value;
}
return sum
}
iteratorSum(arrayIterator([1, 4, 9, 16, 25]))
//=> 55
@raganwald
Copy link
Author

This is a journey... Into sound.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment