I ❤ Ramda - Partial Application with a Special Placeholder
Introduction to I ❤ Ramda
The I ❤ Ramda series will differ slightly from others you may have seen. Instead of using the typical
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.
bind we were able to partially apply the value
2 to the argument
a in the function
multiply to create a completely new function
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
Partial Application with Curry
The Ramda way of handling Partial Application would be to Curry the
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...
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.
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.
fs.readFile has been curried into
readFile, we can use Ramda's Special Placeholder (
__) for the filename argument and partially apply,
😍 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!
The best way to learn is to do 😉. So for this excercise try to create two new functions
fetchPost based off of the function
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
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.
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:
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!
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.