Skip to content

Instantly share code, notes, and snippets.

@laphilosophia
Created November 5, 2018 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laphilosophia/03d79a2bae1e9aac8ef119497463768e to your computer and use it in GitHub Desktop.
Save laphilosophia/03d79a2bae1e9aac8ef119497463768e to your computer and use it in GitHub Desktop.
Javascript Currying and Partial Application Examples
// 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