Skip to content

Instantly share code, notes, and snippets.

@bendc
Created May 21, 2015 08:17
  • Star 25 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
Star You must be signed in to star a gist
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
Copy link
Author

bendc 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