Why use the Ramda library

  • Pointfree is the concept of functional level abstraction, and we often talk about reuse logic, and the key to reuse logic is abstraction, whether it’s object-oriented or architectural design. But few people talk about functional reuse, and when I learned about the Ramda library, I thought it was a good idea. For example

We need to filter the value of isMan: true:

var list = [{ name: 'zhangsan', isMan: true }, { name: 'xiaoli', isMan: false }, ...]
Copy the code

Let’s look at the difference in reusability that comes with writing different processing functions

// Plain JS  
var manList = list.filter(function(task) {
    return task.isMan;
});
Copy the code

Copying code using LoDash is a little easier:

var manList = _.filter(list, {isMan: true});
Copy the code

In both cases, we get a filtered list of tasks. Now with Ramda, we can do this:

var filterManList = R.filter(R.where({complete: false});
Copy the code

As you can see, ramda is actually assembling functions without data, so when you filter data you need to filterManList(list)

In other words, instead of coupling the data to the function, we use a separable function assembly to process the data, which makes it more reusable. Such as below

fn = R.pipe(f1, f2, f3);
Copy the code

The idea that the sum of functions can be arbitrarily combined into a new function, fn, is very similar to the chain of responsibility pattern in object-oriented programming, such as redux middleware composition.

  • It doesn’t really matter what library you use, as long as it helps you quickly accomplish your business needs. Ramda is just an option, and you agree with this programming philosophy.

Learning ramda’s biggest difficulty

  • I also contact time is not long, learn it, I think the biggest problem is that website so much function, let yourself go first screening want to hard to find, it will be great obstacles to continue to use the library’s confidence, so I summarized a mind map chart, according to the needs of the business logic of common like add and delete points of the class, Help students who want to use this library to quickly find the corresponding function

  • This is a really laborious summary, the diagram is large and needs to be saved to a computer (see the usage examples in conjunction with the official documentation ramda.cn/docs/)