Introduction to the Ramda Functional Programming Library

Introduction to the Ramda Functional Programming Library

Daily short news for you
  • Privacy Guides is a non-profit project aimed at providing users with insights into privacy rights, while also recommending best practices or tools to help reclaim privacy in the world of the Internet.

    There are many great articles here, and I will take the example of three concepts that are often confused or misrepresented: Privacy, Security, and Anonymity. While many people who oppose privacy argue that a person does not need privacy if they have 'nothing to hide.' 'This is a dangerous misconception, as it creates the impression that those who demand privacy must be deviant, criminal, or wrongdoers.' - Why Privacy Matters.

    » Read more
  • There is a wonderful place to learn, or if you're stuck in the thought that there's nothing left to learn, then the comments over at Hacker News are just for you.

    Y Combinator - the company behind Hacker News focuses on venture capital investments for startups in Silicon Valley, so it’s no surprise that there are many brilliant minds commenting here. But their casual discussions provide us with keywords that can open up many new insights.

    Don't believe it? Just scroll a bit, click on a post that matches your interests, check out the comments, and don’t forget to grab a cup of coffee next to you ☕️

    » Read more
  • Just got played by my buddy Turso. The server suddenly crashed, and checking the logs revealed a lot of errors:

    Operation was blocked LibsqlError: PROXY_ERROR: error executing a request on the primary

    Suspicious, I went to the Turso admin panel and saw the statistics showing that I had executed over 500 million write commands!? At that moment, I was like, "What the heck? Am I being DDoSed? But there's no way I could have written 500 million."

    Turso offers users free monthly limits of 1 billion read requests and 25 million write requests, yet I had written over 500 million. Does that seem unreasonable to everyone? 😆. But the server was down, and should I really spend money to get it back online? Roughly calculating, 500M would cost about $500.

    After that, I went to the Discord channel seeking help, and very quickly someone came in to assist me, and just a few minutes later they informed me that the error was on their side and had restored the service for me. Truly, in the midst of misfortune, there’s good fortune; what I love most about this service is the quick support like this 🙏

    » Read more

What is Ramda?

Ramda is a library that contains many utility functions to support JavaScript programming. If you have heard of or used lodash before, you can imagine that the functionality of Ramda is similar. If you have never heard of or used either library, keep reading.

The Utility of Ramda

Let's consider a simple problem: getting the "name" property from an object person.

const person = {
  name: "Coffee",  
  age: 18,  
}

Simple, right? We just need to call person.name and we immediately get the data "Coffee". You can use this approach to quickly retrieve data, but what if person has a value of null or undefined? It's best to wrap it in a utility function for reuse.

Now let's make it more flexible by writing a function specifically for retrieving an object property.

function get(obj, attr) {
  return obj[attr];
}

Now, we can call get(person, "name") to get the data "Coffee".

However, this function still needs improvement. If obj is undefined or null, or simply not an object, the get function will throw an error. You can check some conditions before getting the value, but that would just take more time and effort on your part.

Understanding this issue, Ramda has provided a function named prop specifically for getting the value of a property.

const R = require('ramda');

R.prop("name", person); // Coffee
// or
R.prop("name")(person); // Coffee
// or
const getName = R.prop("name");
getName(person); // Coffee

"Oh, if that's all, why do people like using it?" Because prop is just one of many functions that Ramda supports. There are many other functions that provide powerful features which you don't have to spend time rewriting. Furthermore, they can be easily combined with each other to become even more "boss".

For example, combining Ramda functions to filter the objects in the people list that have "age" > 18 and sorting them in descending order by "age".

R.pipe(
  R.filter(
    R.pipe(R.prop('age'), R.lt(18)),  
  ),  
  R.sortWith([
    R.descend(R.prop('age')),  
  ])
)(people)

At first glance, the above code may be long and difficult to understand. To understand and apply Ramda, the only way is to view the descriptions of all the functions on its homepage. Moreover, you also need to understand how curry functions work.

Why Should I Use Ramda?

As mentioned at the beginning of the article, Ramda is a collection of general-purpose utility functions suitable for many problems. One difference from other similar utility libraries is that Ramda follows a functional programming style. Therefore, Ramda is often used in projects that adopt functional programming.

What Makes Ramda Special?

Ramda is designed in a functional programming style, where immutability and pure functions are its core values. This helps you handle code with simplicity and clarity.

Most functions in Ramda support currying. This allows you to easily create new functions from existing ones by not providing their final arguments.

The parameters of functions are ordered to facilitate the application of curried functions. The final data is usually requested last.

Conclusion

This article serves as an introduction to the Ramda utility library for functional programming in JavaScript. Ramda provides many functions focused on solving small problems, and you can easily combine them to create a powerful program.

When Ramda becomes a common language in many projects, it will make deployment and maintenance easier for everyone.

Premium
Hello

Me & the desire to "play with words"

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (1)

Leave a comment...
Avatar
Tùng Nguyễn2 years ago
Ảo quá thấy cách viết này còn khó hiểu hơn
Reply
Avatar
Xuân Hoài Tống2 years ago
@gif [10JhviFuU2gWD6]