Last active
November 12, 2017 15:14
-
-
Save aderaaij/1e3fbc9ddbc465d0e5bf652c91ec8765 to your computer and use it in GitHub Desktop.
https://stackoverflow.com/a/30452949/4474075
The code below is written using ES6 syntaxes but could just as easily be written in ES5 or even less. ES6 is not a requirement to create a "mechanism to loop x times" If you don't need the iterator in the callback, this is the most simple implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A function to repeat whatever you feed it an x amount of time | |
// https://stackoverflow.com/a/30452949/4474075 | |
const times = x => f => { | |
if (x > 0) { | |
f() | |
times (x - 1) (f) | |
} | |
} | |
// use it | |
times (3) (() => console.log('hi')) | |
// or define intermediate functions for reuse | |
let twice = times (2) | |
// twice the power ! | |
twice (() => console.log('double vision')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment