Home > Javascript > Currying and lodash/fp

Currying and lodash/fp

Briefly, currying is a way of constructing functions that allows partial application of a function’s arguments. What this means is that you can pass all of the arguments a function is expecting and get the result, or pass a subset of those arguments and get a function back that’s waiting for the rest of the arguments.

Argument Order

One thing that’s important to keep in mind when currying is the order of the arguments. Using the approach we’ve described, you obviously want the argument that you’re most likely to replace from one variation to the next to be the last argument passed to the original function.

Thinking ahead about argument order will make it easier to plan for currying, and apply it to your work. And considering the order of your arguments in terms of least to most likely to change is not a bad habit to get into anyway when designing functions.

Here is how the lodash/fp documentation describes itself:

The lodash/fp module promotes a more functional programming (FP) friendly style by exporting an instance of lodash with its methods wrapped to produce immutable auto-curried iteratee-first data-last methods.

Compose yourself

The idea of composing functions (or flow in lodash) is to build a larger function out of many smaller ones. The data goes in one end and flows (ah ha!) through each function until it comes out the other side. Each function in the chain passes its return value to the next.

Let’s see an example using lodash/fp:

const flow = require('lodash/fp/flow');
const escape = require('lodash/fp/escape');
const trim = require('lodash/fp/trim');

const sanitise = flow(escape, trim);

sanitise('    <html>    '); // <html>

You can see that we’ve created a sanitise function from escape and trim and when the HTML string is passed in it flows through these two functions before being returned as expected. Again, we’re just being implicit and declaring what we want to happen, not how.

How does currying help?

lodash/fp functions are auto-curried, so when given fewer arguments than expected they give back a function. This works really well with flow:

const flow = require('lodash/fp/flow');
const get = require('lodash/fp/get');
const isEqual = require('lodash/fp/isEqual');

const data = {
  items: 45
};

const hasAllItems = flow(get('items'), isEqual(45));

hasAllItems(data) // true

Here we’re configuring the get and isEqual functions with the arguments needed to get the result, and now they’re waiting for their final argument which is the data. Thanks to flow we can pass that in at one end and let it pass through each curried function until our expected value comes out at the end.

For composition to work each function should be unary (accept one argument) and return a single value for the next function to consume. It’s fine for the first function to be polyadic (takes multiple arguments) as long it returns a single value.

A note on point-free

You may have noticed that in the last two examples you can’t see any mention of data or itemsapart from when it is passed in as an argument. From the Wikipedia page:

Tacit programming, also called point-free style, is a programming paradigm in which function definitions do not identify the arguments (or “points”) on which they operate. Instead the definitions merely compose other functions, among which are combinators that manipulate the arguments.

Advertisements
Categories: Javascript Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: