paint-brush
I ❤ Ramda — Partial Application with a Special Placeholderby@joelthoms
2,096 reads
2,096 reads

I ❤ Ramda — Partial Application with a Special Placeholder

by Joel ThomsMay 11th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The I ❤ Ramda series will differ slightly from others you may have seen. Instead of using the typical <code class="markup--code markup--p-code">foo</code>, <code class="markup--code markup--p-code">bar</code>, <code class="markup--code markup--p-code">baz</code> examples, or articles demonstrating isolated functions, this series aims to showcase real world applications of Ramda.js.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - I ❤ Ramda — Partial Application with a Special Placeholder
Joel Thoms HackerNoon profile picture

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.

Partial Application with a Special Placeholder

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

Using bind we were able to partially apply the value 2 to the argument ain 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.

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, take a look at lines 9 and 10…

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.

Special Placeholder

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

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

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.

😍 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.

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

Homework

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.

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.

Now the meat 🍖

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.

Further 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.

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:

Summary

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.

Cheers!

originally posted on github