Skip to content

Instantly share code, notes, and snippets.

@kalisjoshua
Last active July 1, 2016 15:45
Show Gist options
  • Save kalisjoshua/db0cd621443859391ac7461fad3a472d to your computer and use it in GitHub Desktop.
Save kalisjoshua/db0cd621443859391ac7461fad3a472d to your computer and use it in GitHub Desktop.
Exploration of Curry-ing function in JavaScript

Curry-ing Functions In JavaScript

I don't know why, but this morning I felt like exploring function curry-ing and writing a little bit about it. First I watched a video on curry-ing from the JS Weekly newletter. Then I wanted to write my own implementation. After that I thought I would share what I did with my team at work and then this.

Brain-dump over.

The only reason I created two different implementations is becuase semantically I think that a function is only curried once; as I understand it - to curry - is a process by which you convert a single function which takes multiple arguments into a series of multiple functions each taking exactly one argument.

So, while the implementations work identically from the point of view of the user the differ in their implementation; basically only semantically.

function curry (...setup) {
function apply (fn, ...pre) {
return pre.length < fn.length
? (...post) => apply(fn, ...pre.concat(post))
: fn(...pre);
}
return apply(setup.shift(), ...setup);
}
function curry (fn, ...pre) {
return pre.length < fn.length
? (...post) => curry(fn, ...pre.concat(post))
: fn(...pre);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment