Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the differences and possible similarities of closures and currying?

I've read through some of the post on here about closures and currying but I feel like I didn't find the answer. So what's the differences and possibly the similarities of closures and currying? Thanks for the help :)

like image 906
Nope Avatar asked Dec 17 '08 15:12

Nope


1 Answers

Currying is really a mathematical concept first and foremost. It's the just observation that for any n-ary function f: S0×...Sn → R, you can define a new function fprime (just found a markdown bug!) with n-1 parameters where that first parameter is replaced by a constant. So, if you have a function add(a,b), you can define a new function add1(b) as

add1(b) ::= add(1, b)

...reading "::=" as "is defined to be."

A closure is more of a programming concept. (Of course, everything in programming is a mathematical concept as well, but closures became interesting because of programming.) When you construct a closure, you bind one or more variables; you're creating a chunk of code that has some variables tied to it.

The relationship is that you can use a closure in order to implement currying: you could build your add1 function above by making a closure in which that first parameter is bound to 1.

like image 79
Charlie Martin Avatar answered Nov 11 '22 02:11

Charlie Martin