2019. 5. 4. 22:01ㆍprogramming
I've been using ramdajs over one year. And this article is based on personal experience and thoughs. ramdajs is popular javascript library which has many utility/helper functions. The first time I heard about this library was when I started study functional programming.
ramdajs makes life much easier because by using ramdajs I save typings and can express my code more declarative, and feels like I'm smart person(cooool!) Actually theses characteristics are almost come from functional programming paradigm, cause ramdajs was made for functional programming in javascript.
Let's see few real examples what can I do with this library.
Imagine there is Array of Objects
const people = [
{ name: 'name a', country: 'US', age: 31 },
{ name: 'name b', country: 'FRANCE', age: 22 },
{ name: 'name c', country: 'JAPAN', age: 26 },
{ name: 'name d', country: 'US', age: 17 },
];
And I have to filter "who only lives in 'US'.
The worst code I can ever imagine is iterate over and push the result to some varialbe.
//imperative way
var peoepleWhoLiveInUS = [];
for (var i = 0; i < people.length; i++) {
if (people[i].country === 'US') {
peoepleWhoLiveInUS.push(people[i]);
}
}
maybe(I wish not) there is a bug(I didn't test) cause I have to many many variables and manage states to just filtering.
Next and more functional way to filter is using filter method belonging to Array.prototype
//functional way
var peoepleWhoLiveInUS = people.filter((person) => person.country === 'US');
See? The variables I have to manage is decreased.
Finally using ramda
//functional way with ramdajs
var peoepleWhoLiveInUS = people.filter(R.propEq('country', 'US'));
Ha! at this point you're already big fan of ramda.
And next example let's assume we wanna transform existing Array(same as before) and wanna make new Array only limited fileds.
const people = [
{ name: 'name a', country: 'US', age: 31 },
{ name: 'name b', country: 'FRANCE', age: 22 },
{ name: 'name c', country: 'JAPAN', age: 26 },
{ name: 'name d', country: 'US', age: 17 },
];
The expected result Object of Array will only contains name and age.
//imperative way
var transformed = [];
for (var i = 0; i < people.length; i++) {
transformed.push({ name: people[i].name, age: people[i].age });
}
Again tedious and error prone code.
//functional way
var transformed = people.map((person) => {
return { name: person.name, age: person.age };
});
//or shorten version with es6 syntax
var transformed = people.map(({ name, age }) => {
return { name, age };
});
Again much better.
//functional way with ramdajs
//pick only fields we want
var transformed = people.map(R.pick(['name', 'age']));
//or omit fields we doesn't want
var transformed = people.map(R.omit(['country']));
Seeeeee...
If you still don't know or wanna use ramdajs, I've to give up.
Let's try ramdajs right now, and you will be much familiar with functional way of programming.
'programming' 카테고리의 다른 글
personal review for frontendmasters.com (0) | 2018.10.15 |
---|---|
node.js와 expressjs (0) | 2017.08.09 |