Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Functional loop
const loop = (() => {
const recur = (callback, count, i=0) => {
if (i == count-1) return callback(i);
callback(i);
return recur(callback, count, i+1);
};
return (callback, count) => {
if (count > 0) return recur(callback, count);
};
})();
@bendc

This comment has been minimized.

Copy link
Owner Author

commented May 21, 2015

Example:

const func = n => {
  console.log(n);
  return "Done";
};

const result = loop(func, 3); // 0, 1, 2
console.log(result); // "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.