Skip to content

Instantly share code, notes, and snippets.

@mediaupstream
Created February 25, 2018 03:46
Show Gist options
  • Select an option

  • Save mediaupstream/cf1b8c6c112008bd9bfc536b4c0964b7 to your computer and use it in GitHub Desktop.

Select an option

Save mediaupstream/cf1b8c6c112008bd9bfc536b4c0964b7 to your computer and use it in GitHub Desktop.
repeat a function n number of times.
function repeat(n = 1) {
let i = 0;
return fn => {
while(n--) {
i++;
fn(i)
}
}
}
// example usage:
repeat(10)(i => {
console.log('floop boop', i)
})
@bmoren
Copy link

bmoren commented Feb 25, 2018

Thanks for this. I think this is on track, is there a way (is it even possible) to get rid of the anon function and instead just have curly braces as a direct function? leaving the i out of the mix entirely (at least what would be exposed to the end user of the function!?

so instead of

repeat(10)(i => {
  console.log('floop boop', i)
})

we'd have something like

let i = 0;
repeat(10){
   i++
   console.log('floop boop', i)
}

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