Skip to content

Instantly share code, notes, and snippets.

@railsstudent
Last active January 5, 2022 00:45
Show Gist options
  • Save railsstudent/a66742d0443b35e77755de981edb0851 to your computer and use it in GitHub Desktop.
Save railsstudent/a66742d0443b35e77755de981edb0851 to your computer and use it in GitHub Desktop.
Currying example
function myFavoriteWrestler(promotion, stable, wrestler) {
return `My favorite wrestler is ${wrestler} of ${stable} in ${promotion}.`
}
const momo = myFavoriteWrestler('Stardom', 'Odeo Tai', 'Momo Watanabe')
const slk = myFavoriteWrestler('Stardom', 'Odeo Tai', 'SLK')
const mina = myFavoriteWrestler('Stardom', 'Cosmic Angels', 'Mina')
const waka = myFavoriteWrestler('Stardom', 'Cosmic Angels', 'Waka')
const iroha = myFavoriteWrestler('Marvelous', 'na', 'Iroha')
console.log(momo)
console.log(slk)
console.log(mina)
console.log(waka)
console.log(iroha)
function curryMyFavoriteWrestler(promotion) {
return (stable) => (wrestler) => myFavoriteWrestler(promotion, stable, wrestler)
}
const curryStardom = curryMyFavoriteWrestler('Stardom')
const curryMarvelous = curryMyFavoriteWrestler('Marvelous')
const odeoTai = curryStardom('Odeo Tai')
const cosmicAngels = curryStardom('Cosmic Angels')
const curryMomo = odeoTai('Momo')
const currySLK = odeoTai('SLK')
const curryMina = cosmicAngels('Mina')
const curryWaka = cosmicAngels('Waka')
const curryIroha = curryMarvelous('')('Iroha')
console.log('------------------------')
console.log(curryMomo)
console.log(currySLK)
console.log(odeoTai('Saki'))
console.log(curryStardom('Star')('Hazuki'))
console.log(curryMina)
console.log(curryWaka)
console.log(curryIroha)
// Output
// My favorite wrestler is Momo Watanabe of Odeo Tai in Stardom.
// My favorite wrestler is SLK of Odeo Tai in Stardom.
// My favorite wrestler is Mina of Cosmic Angels in Stardom.
// My favorite wrestler is Waka of Cosmic Angels in Stardom.
// My favorite wrestler is Iroha of na in Marvelous.
// ------------------------
// My favorite wrestler is Momo of Odeo Tai in Stardom.
// My favorite wrestler is SLK of Odeo Tai in Stardom.
// My favorite wrestler is Saki of Odeo Tai in Stardom.
// My favorite wrestler is Hazuki of Star in Stardom.
// My favorite wrestler is Mina of Cosmic Angels in Stardom.
// My favorite wrestler is Waka of Cosmic Angels in Stardom.
// My favorite wrestler is Iroha of in Marvelous.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment