JavaScript currying and functional programming was an introduction to currying. A very useful technique that uses the properties of JavaScript closures – how, where and why to encapsulate argument values to create a new version of a function.

The examples in that were fairly specific. This article will provide a flexible general purpose currying algorithm that you can use to curry anything. The function is available in my cUseful library – but more on that later.

Examples

Sticking to the same kind of examples, using the Properties Service in Apps Script, you might find yourself with a useful function that looks like this.

that can be called like this, to set a key/value pair in a given store

But you can use currying to store some or all of these arguments, so all of these variations do the same thing

That may not seem very useful at first glance, but remember what currying does for you. It creates a brand new function from a function you pass to it, and bakes in some of the arguments you pass to it, so you don’t need them any more.

Assuming that the curry function is from the cUseful library, get a shortcut for it first.

Let’s say that you always want to use the ScriptProperties service, you can shorten your general setPropertyValue function to build in the “script” argument into a new function like this.

Take a bit further, and create a function that has both the “script” store and the “preferredVariety” key already built in.

And you can even recurry already curried functions

Now we’ll update the underlying script to make it handle objects too.

Now curry that and use it for setting objects to the store

Libraries and avoiding permission requests

Another handy thing this gives you is that you can create libraries that don’t know the details of what they are doing, and don’t need a long list of arguments. In Apps Script, if you access some Services it will prompt a request for access to some resource. This means that if you mention for example PropertiesService in a library, everybody that uses it will be asked for permission even though they are not using the part of the library that access properties services. To make dependency free libraries, you can curry things like this in and pass it to the library.

Imagine I have a library that does various things, including this

It has to write some data to a property store against a given key, but you don’t want to pass the property store explicitly, and neither you even want to expose the key against which to write the data, so instead you pass over a curried function that has all that built in. Like this, third party libraries don’t have access to your property store – yet are able to write to a specific key in it!

// handy to pass to a library so the library doesn’t need to get property permissions

The curry function

You can get this in the cUseful library

Mcbr-v4SsYKJP7JMohttAZyz3TLx7pV4j

or on github

It’s very concise, but a little involved. You can simply use it, but it’s included below in case you want to get into how it works.

It’s exposed by this Utils namespace method, which simply calls the curry function, passing on any arguments.

The curry function is expecting at least a function. The function itself is extracted from the argument list, and the remainder of the arguments are considered to be the arguments of the function. A useful property of a function is function.length, which returns the arity (the number of arguments it is expecting) . If the number of arguments received is the same as the number received then it’s time to execute that function normally and return its result, otherwise we need to create a new function, using the curry function itself to work through the arguments.

And that’s a wrap for this deeper dive to currying. If you want more, ping me on the community.

For more like this see Google Apps Scripts Snippets
Why not join our community , follow the blog or follow me on Twitter