Cool post! The motivation behind currying came from lambda calculus. In lambda calculus, you can only have anonymous unary functions. That means your function can only take one parameter and there's no way of calling other functions because they're anonymous, so the only way to do it is by nesting your function calls, a.k.a currying.
Here's the identity function. It takes 1 argument and returns it.
λx.x
In pseudo javascript this looks like
(x)=>x
What if we want the Kestrel/K combinator? In javascript, it can be expressed like this
(x,y)=>x
But how would we do that in lambda calculus? By calling another lambda expression.
λx.(λy.x)
In javascript, this will look like
(x)=>(y)=>x
So, in javascript it looks like you can just have more arguments in your function. Why would you want to do currying if it's capable of taking in more than one argument? Well.. the cool thing about curried functions combined with naming them, is you can partially apply them.
constaddNum=x=>y=>x+yconstaddOne=addNum(1)// y => 1 + yconstmyNum=addOne(10)// 11constmyNiceNum=addOne(69)// 70
And because you're using unary functions it's easier to identify the cardinality of each function.
EDIT: Using the above example with a compose function... coz partially applied functions with composition is pretty cool
Thanks for sharing the backstory behind currying! Now, I'll definitely keep an eye out when Lambda calculus comes up in my college. I'll have something to relate to! 💙
Cool post! The motivation behind currying came from lambda calculus. In lambda calculus, you can only have anonymous unary functions. That means your function can only take one parameter and there's no way of calling other functions because they're anonymous, so the only way to do it is by nesting your function calls, a.k.a currying.
Here's the identity function. It takes 1 argument and returns it.
In pseudo javascript this looks like
What if we want the Kestrel/K combinator? In javascript, it can be expressed like this
But how would we do that in lambda calculus? By calling another lambda expression.
In javascript, this will look like
So, in javascript it looks like you can just have more arguments in your function. Why would you want to do currying if it's capable of taking in more than one argument? Well.. the cool thing about curried functions combined with naming them, is you can partially apply them.
And because you're using unary functions it's easier to identify the cardinality of each function.
EDIT: Using the above example with a compose function... coz partially applied functions with composition is pretty cool
Thanks for sharing the backstory behind currying! Now, I'll definitely keep an eye out when Lambda calculus comes up in my college. I'll have something to relate to! 💙
I'll echo that as I was struggling to understand why you would bother. Of course, lambda (something I don't yet use a lot) makes perfect sense.