Created
January 3, 2019 21:29
-
-
Save miladvafaeifard/d3b86b11298d50ee10ddd36d634bfc55 to your computer and use it in GitHub Desktop.
js functional programming
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
const once = (fn) => { | |
return (...args) => { | |
fn && fn(...args); | |
fn = null | |
} | |
} | |
const thisManyTimes = (fn, limit) => { | |
return (...args) => { | |
if(limit > 0) { | |
limit--; | |
return fn(...args); | |
} | |
} | |
} | |
let log = once(console.log); | |
log('hello'); | |
log('hello'); | |
log('hello'); | |
log = thisManyTimes(console.log, 2); | |
log('Test'); | |
log('Test'); | |
log('Test'); | |
log('Test'); | |
log('Test'); | |
log('Test'); | |
log('Test'); | |
log('Test'); | |
const factUp = (n, f = 1) => n <= f? f : f * factUp(n , f + 1); | |
console.log(factUp(4)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment