Created
November 5, 2018 12:10
-
-
Save laphilosophia/03d79a2bae1e9aac8ef119497463768e to your computer and use it in GitHub Desktop.
Javascript Currying and Partial Application Examples
This file contains 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
// Javascript Currying and Partial Application Examples | |
const multiplier = x => { | |
return (y, z) => { | |
return z * y * z | |
} | |
} | |
const mult = multiplier(10) // x | |
mult(20, 30) // y, z | |
// great example | |
const discount = discount => { | |
return price => { | |
return price * discount | |
} | |
} | |
const tenPercentDiscount = discount(0.1) // 10% | |
const twentyPercentDiscount = discount(0.2) // 20% | |
tenPercentDiscount(500) | |
twentyPercentDiscount(650) | |
// General Curry Function | |
function curry (fn, ...args) { | |
return (..._arg) => { | |
return fn(...args, ..._arg) | |
} | |
} | |
function volume (l, h, w) { | |
return l * h * w | |
} | |
const hCy = curry(volume, 100) | |
hCy(200, 900) | |
hCy(70, 60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment