0

Today I have found a strange way of adding 2 numbers in javascript:

Basically the call of the method looks this way: add(5)(6) and the declaration of the method is:

function add(x) {
  return function(y) { return x + y; };
}

Ok, I got it. The add function does not return a number, but returns the anonymous function which adds the first number with the second. But what is the point (how it can be helpful)?

Where exactly can it be useful apart from puzzling other people or writing some sort of obfuscated malware?

Evan Davis
  • 35,493
  • 6
  • 50
  • 57
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • 3
    Are you asking what the point of being able to return functions is, or are you asking what the point of having a curried `add` function is? This particular case might not be the most universally useful thing, but it’s not supposed to be. – Ry- Oct 02 '14 at 23:23
  • My guess is they thought chaining the argument sets together like that instead of writing the function name more than once looked cooler. :) – Cody Stott Oct 02 '14 at 23:24
  • @minitech I am asking how foo(a)(b) can be helpful. Add function is just an example. Thank you for telling the name of this technique. – Salvador Dali Oct 02 '14 at 23:24
  • 2
    It's a functional programming concept called currying. The `add` or `sum` function is just a common example used to describe the concept. [search Google](https://www.google.com/#q=functional+javascript+currying) – km6zla Oct 02 '14 at 23:25
  • related: [When to use currying and partial functions in JavaScript](http://stackoverflow.com/questions/7932954/when-to-use-currying-and-partial-functions-in-javascript) – Bergi Oct 02 '14 at 23:37
  • I want to fix this awful title, but I'm still not sure what you're actually asking. Per the answers, `foo(a)(b)` _isn't_ useful, but returning a curried function for use later is very useful; think `bar = foo(a); bar(b);` instead of `foo(a)(b)`. – Evan Davis Oct 02 '14 at 23:43
  • Suggested title, something along the lines of "When is it useful to do currying?". I still think that even with a better title it's still too broad. The last paragraph of the question could also easily lead to an opinion-based discussion because it is a loaded question. – ivarni Oct 03 '14 at 05:36
  • But since there apparently is a dupe that hasn't been closed (and even upvoted) maybe it's still on-topic. – ivarni Oct 03 '14 at 05:42

3 Answers3

1

Lets have a look at a function which is bit more common in the wild, pluck:

function pluck(prop) {
    return function(obj) {
        return obj[prop];
    }
}

This function accepts a property name and returns a new function which, given an object, returns the property of that object.

This is useful for mapping an array of objects to an array of a specific property of those objects:

var names = people.map(pluck('name'));

Now, if you just have to do this a single time, there is no need to create a dedicated pluck function. However, if you do this multiple times, it avoids code repetition.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
1

Obviously, the call add(5)(6) is not too useful - you'd rather write 5 + 6, or, if it's really a function, add(5, 6).

The great benefit is in functional programming where you are supposed to pass a function around (callback!) - most prominent in the array utils. For example:

[1, 2, 3].map(add(5)) // [6, 7, 8]

You can also use this for the composition of quite complex functions, without having to deal with function expressions. If we didn't have this curried add function, in the above example we would have needed to write

[1, 2, 3].map(function(y) {
     return 5 + y;
}) // [6, 7, 8]

…and that every time for each different x.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

You wouldn't necessarily use it as you've described, but perhaps if you want to pass around a function that "adds 5 to the result", you could do

var addsFive = add(5);
// some other code, perhaps passing this var to another function
var result = addsFive(6); // returns 11

This technique of "partial" function invocation is called currying - this other SO question goes into some details on how/where it can be useful, as well as provides several links to help explain the concept.

Community
  • 1
  • 1
Krease
  • 15,805
  • 8
  • 54
  • 86
  • Hello anonymous downvoter - is there something incorrect or that needs improvement about this answer? – Krease Oct 03 '14 at 17:03