1

In order words, what's the advantage of writing this:

Listing 1

var sortBy = function sortBy(p) {
  return function (a, b) {
    return a[p] > b[p];
  };
};

Compared to this?

Listing 2

var sortBy = function sortBy(p, a, b) {
  return a[p] > b[p];
};

Reference.

candied_orange
  • 119,268
alex
  • 383

2 Answers2

6

In the first listing you can partially apply sortBy, in the second you cannot. The advantage of partially applying this function is that you can decouple the dependency of which key to sort by from usages of the sort function. See this question for a more complete description of the advantages of currying.

For example:

const sortByName = sortBy("name")
const a = { name: "aaron" }
const b = { name: "billy" }
const c = { name: "charlie" }
sortByName(a, b) // true
sortByName(b, c) // true

This is useful if you need a binary function. For example Array.prototype.sort() takes a binary function as an argument. You could do something like this (if your sort function was implemented according to the spec):

[a, b, c].sort(sortByName)

but with listing 2 you would have to do something like:

[a, b, c].sort((left, right) => sortBy("name", left, right))
Samuel
  • 9,237
1

Well now, you're asking two different questions.

What's the benefit returning a function compared to a value? Functions can do things. Values just sit there being things. Unless of course the value is a function.

With listing 1 you can say sortBy(0)(x, y) With listing 2 you have to say sortBy(0, x, y)

Things get fun with listing 1 when you say s = sortBy(0) because now you can say s(x,y) or s(y,z). It's remembered the 0 so you don't have to pass it when you call s. That means the benefit is that you can know these values at different times. You don't have to shove them all in at once.

What you've got hold of there in listing 1 is a little thing called a closure. It's almost the same as an object. The first call acts like a constructor. The second is call is like a method call. The difference is that closures only have one method. So you don't have to know it's name. You give it a name and just throw another () after it.

Of course the functional people hate to hear them called methods. It's not a method it's a closure that "has access to it's enclosing lexical scope". Feh, it's a fancy way to say the vars outside of the inner function the same way a method has access to instance member vars. In this case it's the p that was passed in.

Unlike with objects you get to decide what the method, sorry, closure, is called.

candied_orange
  • 119,268