Skip to content

Instantly share code, notes, and snippets.

@bendc
Created May 21, 2015 08:17
Show Gist options
  • Save bendc/6cb2db4a44ec30208e86 to your computer and use it in GitHub Desktop.
Save bendc/6cb2db4a44ec30208e86 to your computer and use it in GitHub Desktop.
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