JavaScript Currying: Its really just reduce over variadic input. …and its not that hard.

const sum2 = (a,b) => a + b
const sum3 = (a,b,c) => a + b + c
const sum2 = a => b => a + b
const sum3 = a => b => c => a + b + c
// if sum2 is in curried form
const incrA = sum2(1)
// if sum2 is in tupled form
const incrB = (b) => sum2(1, b)
const curry2 = f => a => b => f(a, b)
const sum2Tupled = (a,b) => a + b
const sum2Curried = curry2(sum2Tupled)
  • variadic input
  • spread operator
  • reduce
  • and papply
// consider sum2 defined in tupled form
const incr = papply(sum2, 1)
const papply = (f, x) => f.bind(null, x)
const head = xs => xs[0]

const tail = xs => xs.slice(1)

const reduce = (f, agg, list) =>
(list.length == 0) ?
agg :
reduce(f, f(agg, head(list)), tail(list)) ;
const curry = f =>
(f.length == 0) ?
f() :
(...xs) => curry(reduce(papply, f, xs)) ;
const add2 = (a,b) => a + b// using a list processing library
const sumList = list => reduce(add2, 0, list)
reduce(papply, f, xs)
  1. the attempt to curry a function that doesn’t take input
  2. the application of more parameters than the captured function intends to take

--

--

--

A 25 year software industry veteran with a passion for functional programming, architecture, mentoring / team development, xp/agile and doing the right thing.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Javascript: Event binding on dynamically created elements

JavaScript: Closures and the Call Stack

javascript30.com — Day 1: Learning and relearning vanillajs

How to Build Serverless Vue Applications with AWS Amplify

JavaScript ES6

7 Things I Wish I Knew When I Started Learning How to Code

How can you become a good programmer?

NodeJS on Android [Root]

https://medium.com/@2771254/list/a2058c8adbc0

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Todd Brown

Todd Brown

A 25 year software industry veteran with a passion for functional programming, architecture, mentoring / team development, xp/agile and doing the right thing.

More from Medium

JavaScript Salaries, Rates, and Team Costs Review Comparison USA and Ukraine

What is recursive function?

What are WebSockets?

The Trials and Tribulations of Asynchronous Code