I ❤ Ramda - Partial Application with a Special Placeholder

in #javascript3 years ago (edited)

Introduction to I ❤ Ramda

The I ❤ Ramda series will differ slightly from others you may have seen. Instead of using the typical foo, bar, baz examples, or articles demonstrating isolated functions, this series aims to showcase real world applications of Ramda.js.

This is the first article and will focus on Partial Application with a Special Placeholder.

Partial Application with Bind

If you have used bind, you may already be familiar with the concept of Partial Application.

code snippet 01

Using bind we were able to partially apply the value 2 to the argument a in the function multiply to create a completely new function double.

The above solution (with bind) works just fine, even if we do have to call bind and also pass in the context (in this case it's null).

Partial Application with Curry

The Ramda way of handling Partial Application would be to Curry the multiply function.

code snippet 02

Okay, so now the code is... different, but you're not excited, and I get that. But hang on because the 🧙 magic is just about to start.

Where bind falls short

Let's consider this example...

code snippet 03

All calls to readFile are going to be utf8 and now that I think about it, they are all going to use the same output function too.

For this example bind cannot be used because bind will only apply arguments from left to right. A completely new function would have to be created.

code snippet 04

Special Placeholder

Ramda has what they refer to as a Special Placeholder which is used to to partially apply arguments regardless of position.

To use this Special Placeholder, the function must first be curried.

code snippet 05

Now that fs.readFile has been curried into readFile, we can use Ramda's Special Placeholder (__) for the filename argument and partially apply, utf8 and output to readFile.

code snippet 06

😍 If you are not excited at this point, go see a doctor because you dead inside.

Let's finish this example up with some refactoring.

code snippet 07

readFile can also be used like this because now we have more options!

code snippet 08


The best way to learn is to do 😉. So for this excercise try to create two new functions fetchGet and fetchPost based off of the function fetch.

code snippet 09

If you need help, post questions in the comments section, I'll do my best to answer every question!

What else can we do?

Here's a simplified snippet from a project I am working on. First I'll stub out the tokenContract so you can also run this example.

Again, don't read too much into this part of the code, it's just the setup. But notice to how the curry function is used below.

code snippet 10

Now the meat 🍖

code snippet 11

Curry and Partial Application allowed us to trim i => and , i off the ends of our function inside then. This is a pattern you will see often. The single argument supplied from the then function (in this case it's i) will become the final argument of the function to be called in tokenContract.getTokenAtIndex.

Futher research: The process of omitting the i at the end is called point-free or tacit programming. The argument i being the "point".

And since I know the work I am doing will always be for account, I could apply the account value to both functions.

code snippet 12

Extra credit

Function composition will be covered in future articles, but right now I'd like leave this as extra credit for you to research and tinker with:

code snippet 13


This article briefly covered Partial Application, Currying, and even Function Composition. Head over to Ramda to learn more. Ask questions in the comments, I will respond to every question!

If you found this interesting, I have more articles on Medium and dev.to.

Subscribe on Twitter, Medium, dev.to and steemit or joel.net to be notified of future articles.

For other articles in this series, check here: https://github.com/joelnet/articles



Because you cannot edit articles a week after posting on Steemit, view the article on github for the most up to date version of this article. You can also copy and paste code from there.

license CC BY 4.0


Congratulations @joelnet! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

The Steem blockchain survived its first virus plague!
Vote for @Steemitboard as a witness to get one more award and increased upvotes!