Thursday, March 12, 2015

Favoring Curry


My recent article on functional composition in Ramda breezed over an important topic. In order to do the sort of composition we would like with Ramda functions, we need these functions to be curried.
Curry? Like the spicy food? What? Where?
Actually, curry is named for Haskell Curry, who was one of the first to investigate such techniques. (Yes, they used his first name for a functional programming language too; not only that, but Curry's middle initial was 'B', which of course stands for Brainf*ck.)
Currying is the process of turning a function that expects multiple parameters into one that, when supplied fewer parameters, returns a new function that awaits the remaining ones.
The basics look like this. Here's a plain function:
  1. // uncurried version
  2. var formatName1 = function(first, middle, last) {
  3. return first + ' ' + middle + ' ' + last;
  4. };
  5. formatName1('John', 'Paul', 'Jones');
  6. //=> 'John Paul Jones' // (Ah, but the musician or the admiral?)
  7. formatName1('John', 'Paul');
  8. //=> 'John Paul undefined');
But a curried version behaves more usefully:
  1. // curried version
  2. var formatNames2 = R.curry(function(first, middle, last) {
  3. return first + ' ' + middle + ' ' + last;
  4. });
  5. formatNames2('John', 'Paul', 'Jones');
  6. //=> 'John Paul Jones' // (definitely the musician!)
  7. var jp = formatNames2('John', 'Paul'); //=> returns a function
  8. jp('Jones'); //=> 'John Paul Jones' (maybe this one's the admiral)
  9. jp('Stevens'); //=> 'John Paul Stevens' (the Supreme Court Justice)
  10. jp('Pontiff'); //=> 'John Paul Pontiff' (ok, so I cheated.)
  11. jp('Ziller'); //=> 'John Paul Ziller' (magician, a wee bit fictional)
  12. jp('Georgeandringo'); //=> 'John Paul Georgeandringo' (rockers)
or
  1. ['Jones', 'Stevens', 'Ziller'].map(jp);
  2. //=> ['John Paul Jones', 'John Paul Stevens', 'John Paul Ziller']
And you can do this in multiple passes, as well:
  1. var james = formatNames2('James'); //=> returns a function
  2. james('Byron', 'Dean'); //=> 'James Byron Dean' (rebel)
  3. var je = james('Earl'); also returns a function
  4. je('Carter'); //=> 'James Earl Carter' (president)
  5. je('Jones'); //=> 'James Earl Jones' (actor, Vader)
(Some will insist that what we're doing is more properly called "partial application", and that "currying" should be reserved for the cases where the resulting functions take one parameter, each resolving to a separate new function until all the required parameters have been supplied. They can please feel free to keep on insisting.)

Booooooring! What Can You Do For ME?

Here is a slightly more meaningful example. If you want to find the sum of a collection of numbers, you could do this:
  1. // Plain JS:
  2. var add = function(a, b) {return a + b;};
  3. var numbers = [1, 2, 3, 4, 5];
  4. var sum = numbers.reduce(add, 0); //=> 15
And if you wanted to write a generic function that would total any list of numbers, you might write:
  1. var total = function(list) {
  2. return list.reduce(add, 0);
  3. };
  4. var sum = total(numbers); //=> 15
In Ramda, total and sum are very similar. You can define sum like this:
  1. var sum = R.reduce(add, 0, numbers); //=> 15
But because reduce is a curried function, when you skip the last parameter, as in the definition oftotal:
  1. // In Ramda:
  2. var total = R.reduce(add, 0); // returns a function
you simply get back a function you can call:
  1. var sum = total(numbers); //=> 15
Note again just how similar the definition of the function and the application of that function to data can be:
  1. var total = R.reduce(add, 0); //=> function:: [Number] -> Number
  2. var sum = R.reduce(add, 0, numbers); //=> 15

Don't Care: I'm Not a Math Geek!

So you do web development, huh? You make AJAX calls to the server? You're using Promises, I hope? Do you have to manipulate the data that comes back, filter it, subset it? Or you do server-side development? You asynchronously query a no-SQL database, and manipulate those results?
The best thing I can suggest is that you go look at Hugh FD Jackson's excellent post, Why Curry Helps. It's the best reading I've seen on this. And if you're a visual learner, spend half an hour on Dr. Boolean's video Hey Underscore, You're Doing It Wrong. (And don't worry about the title; he doesn't spend too much time actually trashing libraries in there.)
Really go ahead. Look at those; they can explain better than I can; you already can see that I'm verbose, windy, wordy and downright garrulous. And if you've seen those, you can skip the rest of this section. They've already said it better than I can.
You've been warned.

Suppose we expect to get some data like this:
  1. var data = {
  2. result: "SUCCESS",
  3. interfaceVersion: "1.0.3",
  4. requested: "10/17/2013 15:31:20",
  5. lastUpdated: "10/16/2013 10:52:39",
  6. tasks: [
  7. {id: 104, complete: false, priority: "high",
  8. dueDate: "2013-11-29", username: "Scott",
  9. title: "Do something", created: "9/22/2013"},
  10. {id: 105, complete: false, priority: "medium",
  11. dueDate: "2013-11-22", username: "Lena",
  12. title: "Do something else", created: "9/22/2013"},
  13. {id: 107, complete: true, priority: "high",
  14. dueDate: "2013-11-22", username: "Mike",
  15. title: "Fix the foo", created: "9/22/2013"},
  16. {id: 108, complete: false, priority: "low",
  17. dueDate: "2013-11-15", username: "Punam",
  18. title: "Adjust the bar", created: "9/25/2013"},
  19. {id: 110, complete: false, priority: "medium",
  20. dueDate: "2013-11-15", username: "Scott",
  21. title: "Rename everything", created: "10/2/2013"},
  22. {id: 112, complete: true, priority: "high",
  23. dueDate: "2013-11-27", username: "Lena",
  24. title: "Alter all quuxes", created: "10/5/2013"}
  25. // , ...
  26. ]
  27. };
And we need a function getIncompleteTaskSummaries that accepts a membername parameter, then fetches the data from the server (or somewhere), chooses the tasks for that member that are not complete, returns their ids, priorities, titles, and due dates, sorted by due date. Actually, it returns a Promise that should resolve into that sort of list.
If you pass "Scott" to getIncompleteTaskSummaries, it might return
  1. [
  2. {id: 110, title: "Rename everything",
  3. dueDate: "2013-11-15", priority: "medium"},
  4. {id: 104, title: "Do something",
  5. dueDate: "2013-11-29", priority: "high"}
  6. ]
Ok, here we go. Does code like this look at all familiar?
  1. getIncompleteTaskSummaries = function(membername) {
  2. return fetchData()
  3. .then(function(data) {
  4. return data.tasks;
  5. })
  6. .then(function(tasks) {
  7. var results = [];
  8. for (var i = 0, len = tasks.length; i < len; i++) {
  9. if (tasks[i].username == membername) {
  10. results.push(tasks[i]);
  11. }
  12. }
  13. return results;
  14. })
  15. .then(function(tasks) {
  16. var results = [];
  17. for (var i = 0, len = tasks.length; i < len; i++) {
  18. if (!tasks[i].complete) {
  19. results.push(tasks[i]);
  20. }
  21. }
  22. return results;
  23. })
  24. .then(function(tasks) {
  25. var results = [], task;
  26. for (var i = 0, len = tasks.length; i < len; i++) {
  27. task = tasks[i];
  28. results.push({
  29. id: task.id,
  30. dueDate: task.dueDate,
  31. title: task.title,
  32. priority: task.priority
  33. })
  34. }
  35. return results;
  36. })
  37. .then(function(tasks) {
  38. tasks.sort(function(first, second) {
  39. var a = first.dueDate, b = second.dueDate;
  40. return a < b ? -1 : a > b ? 1 : 0;
  41. });
  42. return tasks;
  43. });
  44. };
Now wouldn't it be nicer if the code looked more like this?:
  1. var getIncompleteTaskSummaries = function(membername) {
  2. return fetchData()
  3. .then(R.get('tasks'))
  4. .then(R.filter(R.propEq('username', membername)))
  5. .then(R.reject(R.propEq('complete', true)))
  6. .then(R.map(R.pick(['id', 'dueDate', 'title', 'priority'])))
  7. .then(R.sortBy(R.get('dueDate')));
  8. };
If so, then currying could well be for you. All the Ramda functions mentioned in this block are curried. (In fact, pretty well all Ramda functions of more than one parameter are curried, with only a few necessary exceptions.) In every case the currying is part of what makes it so easy to compose them into such a nice, elegant block.
Let's take a look at what's happening.
get (also known as prop) is defined like this:
  1. ramda.get = curry(function(name, obj) {
  2. return obj[name];
  3. });
But when we call it above, we supply only the first parameter, name. As we discussed, this means that we will get back a function that is waiting for the obj parameter to be passed by the firstthen. That means that this:
  1. .then(R.get('tasks'))
can be thought of as simple shorthand for
  1. .then(function(data) {
  2. return data.tasks;
  3. })
Next is propEq, which is defined as:
  1. ramda.propEq = curry(function(name, val, obj) {
  2. return obj[name] === val;
  3. });
So when we call it with parameters "username" and membername (the latter was supplied to our function as a parameter), the currying gives us back a new function, something equivalent to
  1. function(obj) {
  2. return obj['username'] === membername;
  3. }
where the value of membername is bound to the value that was passed to us.
This function is then passed into filter.
Ramda's filter works much like the native filter on Array.prototype, but the signature is
  1. ramda.filter = curry(function(predicate, list) { /* ... */ });
So we're curried yet again, passing in only the predicate, and not the list of tasks passed through from the previous step. (I did tell you that everything was curried, did I not?)
We do the same sort of thing with propEq('complete', false) -> reject as we did withpropEq('username', membername) -> filter. Reject is the same as filter except that it reverses the sense of it. It keeps only those ones for which the predicate returns false.
Ok, are you still here reading? My index fingers are getting tired. (Really have to learn to touch-type!) You don't really need me to explain those last two lines, right? Really? You're sure? All right! All right! Yes! ... Yes, I said I would!
So next we see
  1. R.pick(['id', 'dueDate', 'title', 'priority'])
pick accepts a list of property names and an object, and returns a new object with those properties copied from the original. But lo and behold, we're curried again. And since we only pass the list of property names, we get a function that will return such a new object once we supply it an object. That function gets passed into R.map. As with filter, this works much like the native Array prototype version, but with the signature:
  1. ramda.map = curry(function(fn, list) { /* ... */ });
And the broken record here will report yet again -- I told you I'd be tedious -- that this function is curried, since we only supply the function from the (curried!) output of pick to this, and not the list. then will invoke this with the list of tasks.
OK, remember sitting in school, waiting for the for the class to end? The minute hand on the clock was stuck, and the second hand was moving through molasses? The teacher was droning on and on about the same thing over and over. Remember that? And then there was that moment, maybe two minutes before the end of the period, when the end was suddenly in sight:Hallelujah! I think we're there with this example. There is only this left:
  1. .then(R.sortBy(R.get('dueDate')));
We already talked about get. Curried like this, it returns a function that, given an object, returns its dueDate property. We pass this into sortBy, which takes a function such as this and a list and sorts the list based on the values returned by the function against the elements of the list. But wait, we don't have a list, right? Of course not. We're curried again. But when we're invoked by.then(), it will receive the list, passing each object to get, and sorting based on the results.

So How Important Is The Currying?

This example is demonstrating the Ramda utility functions alongside the currying aspects of Ramda. Perhaps the currying is not really that important. Let's try to rewrite that without the currying:
  1. var getIncompleteTaskSummaries = function(membername) {
  2. return fetchData()
  3. .then(function(data) {
  4. return R.get('tasks', data)
  5. })
  6. .then(function(tasks) {
  7. return R.filter(function(task) {
  8. return R.propEq('username', membername, task)
  9. }, tasks)
  10. })
  11. .then(function(tasks) {
  12. return R.reject(function(task) {
  13. return R.propEq('complete', true, task);
  14. }, tasks)
  15. })
  16. .then(function(tasks) {
  17. return R.map(function(task) {
  18. return R.pick(['id', 'dueDate', 'title', 'priority'], task);
  19. }, tasks);
  20. })
  21. .then(function(abbreviatedTasks) {
  22. return R.sortBy(function(abbrTask) {
  23. return R.get('dueDate', abbrTask);
  24. }, abbreviatedTasks);
  25. });
  26. };
That, I think, is the equivalent. It's still better than the original code. Ramda's utility functions have some -- er, utility -- even in the absence of currying. But I don't think it's even close to as readable as this:
  1. var getIncompleteTaskSummaries = function(membername) {
  2. return fetchData()
  3. .then(R.get('tasks'))
  4. .then(R.filter(R.propEq('username', membername)))
  5. .then(R.reject(R.propEq('complete', true)))
  6. .then(R.map(R.pick(['id', 'dueDate', 'title', 'priority'])))
  7. .then(R.sortBy(R.get('dueDate')));
  8. };
And that is why we curry.

Here endeth the lesson.
I did warn you.
Next time, when I tell you to read someone else's stuff instead of mine, you'll pay attention, right? It might be too late to look at these instead of reading mine, but they still are very well done, and maybe you can do it as well:
There's one other that's brand-new. I just saw it today. We'll see if it stands the test of time, but for now it's a good read:

A Dirty Little Secret

Currying, as powerful as it is, is not enough to make your code elegant.
There seem to be three important components.
  • Last time I discussed functional composition. That is necessary for bringing together all your beautiful ideas without a lot of ugly glue code to hold them together.
  • Currying is useful both because it's needed to support composition and because it removes tremendous amounts of boilerplate as we see above.
  • A collection of utility functions operating on useful data structures, such as lists of objects.
One of the goals of Ramda is to provide all these in a convenient package.

No comments:

Post a Comment