5 ms·
Ok, it might be too early in the morning, but I still don't understand this. I read that post, and the post by 'raganwald that the former linked to, and I still
by TeMPOraL 9y ago
Ok, it might be too early in the morning, but I still don't understand this. I read that post, and the post by 'raganwald that the former linked to, and I still don't see any difference between currying and partial application. And yet both posts seem to imply (without showing in any way) that the two concepts are different. How come?
- Tade0 9y agoHere's a nice write-up that originally helped me understand currying: https://hughfdjackson.com/javascript/why-curry-helps/ https://hughfdjackson.com/javascript/why-curry-helps/ Basically a curried function could be described nested series of partially applied functions(one argument per step). On a side note to me this article is a bit on the heavy side when it comes to conveying the message. For one thing partial application should be explained first.
- tempodox 9y agoPartial application (passing too few arguments to a function) is something that a programmer does. Function currying is something that a programming language does in response to partial application. It's basically wrapping the partially applied function in another one that expects the missing arguments and then performs the originally intended computation.
- catnaroek 9y agoCurrying is something the programmer has to do when defining a function - well before partial application happens at all! More precisely, the programmer has to choose between: fun foo (x, y) = ... And fun foo x y = ... The language won't make the choice for them.
- k__ 9y agoPartial application is applied on call site, currying at definition. Currying: const f = x => y => x + y; const g = f(1); const sum = g(2); Partial application: const f = (x, y) => x + y; const g = _ => f(1, _); const sum = g(2); Most FP languages have nicer syntax for this.
- gmfawcett 9y agoThat's not what currying is. You've just used partial application in two different contexts. Currying takes a multi-argument function: const f = (x, y, z) => x + y * z; ...and converts it to a function that takes a single argument, and returns a function taking the next argument, recursively: const f_curried = x => y => z => x + y * z; const result = f_curried(1)(2)(3) edit: maybe you were trying to convey that, in your first example, f is already a curried function? As a whole, the first example just looks like a partial-application demonstration.
- chriswarbo 9y agoPartial application introduces one new closure, to capture some arguments now and allow the rest to be supplied later: f = function (x, y, z) { return [x, y, z]; }; g = partiallyApply(f, 1, 2); With these definitions (and some suitable definition of `partialApply`): g(3) == f(1, 2, 3) == [1, 2, 3] Note that `g` is a closure: a function with (references to) some values: the function `f`, and the data `x = 1` and `y = 2`. This closure will accept some argument (`z`) and return `f(x, y, z)`. Whilst currying is similar, it's like partially-applying everything, and doing so implicitly. For example, given `h = curry(f)` we get a new function: a closure with a reference to `f`. If we say `i = h(1)`, we get a new closure which references `f` and `x = 1`. We can say `j = i(2)` to get another new closure which references `f`, `x = 1` and `y = 2`. All of the following hold: j(3) == i(2, 3) == h(1, 2, 3) == f(1, 2, 3) == [1, 2, 3] The important thing to notice is that in the case of partial-application, we only got one extra closure, and that happened when we explicitly called the `partialApply` function. In the case of currying, we only called the `curry` function once, to get `h`. Yet we subsequently get more and more closures, without any extra calls to the `curry` function: `h` can return new closures, and those closures can return new closures (like `i` and `j`) and so on. In a language with strict (call by value) semantics like Javascript, this makes it hard to know when the "real" function (in our case `f`) will actually get called. This is important if `f` has side-effects, or involves some heavy computation. It also makes it harder to know when values will be garbage collected, due to the references inside these closures. In the case of `partialApply`, only one closure is created, we know exactly where (at the point where `partialApply` is called), we know exactly what references will be captured (the arguments to `partialApply`) and we know exactly when the "real" function will get called (it will be called whenever the resulting closure is called). To add to all of the other references in this thread, I also wrote my own a few years ago :) Here's a basic implementation of currying in Javascript: http://chriswarbo.net/blog/2012-02-20-currying_in_javascript.html http://chriswarbo.net/blog/2012-02-20-currying_in_javascript... Here's an "improvement" which also 'extends' the currying to return values: http://chriswarbo.net/blog/2012-10-01-better_currying_in_javascript.html http://chriswarbo.net/blog/2012-10-01-better_currying_in_jav... For example, if we have the following: a = curry(function(w, x) { return function(y) { return function(z) { return w + x + y + z; }; }; }); The "basic" version of `curry`, which is what I've seen most tutorials use, handles situations with "too few" arguments, by returning a closure: a(1)(2)(3)(4) == a(1, 2)(3)(4) == 1 + 2 + 3 + 4 In the "improved" version we also handle situations with "too many" arguments, by passing them as arguments to the return value (of course, this assumes we're returning functions, like above); which makes all of the following equivalent: a(1)(2)(3)(4) == a(1, 2)(3)(4) == a(1)(2, 3)(4) == a(1)(2)(3, 4) == a(1, 2, 3)(4) == a(1)(2, 3, 4) == a(1, 2, 3, 4) == 1 + 2 + 3 + 4 I've also written an equivalent for PHP, which goes more into the difference between partial application and currying: http://chriswarbo.net/blog/2014-02-21-partial_application___currying_in_php.html http://chriswarbo.net/blog/2014-02-21-partial_application___...