[Ramda] Refactor to Point Free Functions with Ramda using compose and converge
In this lesson we'll take some existing code and refactor it using some functions from the Ramda library, most notably, compose and converge. When we're done, we'll have taken a function with a couple of local variables and parameter references and converted it into more streamlined "point-free" or "tacit" functions.
For example, we have following function:
const R = require('ramda');
const person = {
id: ,
name: 'Joe'
};
const generateUrl = (id) => `http://img.soicalnetwork.com/avatar/${id}.png`;
const getUpdatedPerson = (person) => {
const url = generateUrl(person.id);
return R.assoc('avatar', url, person);
}
const result = getUpdatedPerson(person);
It will add a 'avatar' prop to person object.
We want to refactor the code to make it point free style, we the functions can be more reuseable and easy to understand.
First try:
//===============================================
// #1 Refactoring
//===============================================
/*
Solve the problem that when id is undefined, we need a default image
Solution: propOr('defaultValue', 'prop')
*/ const generateUrl = (id) => `http://img.soicalnetwork.com/avatar/${id}.png`;
const getUpdatedPerson = (person) => {
const url = generateUrl(R.propOr('default', 'id')(person));
return R.assoc('avatar', url, person);
}
const result = getUpdatedPerson(person);
console.log(result);
Here we use 'R.propOr', to get prop of an object and set default fallback value. This can help us prevent undefined problem.
Second try:
//===============================================
// #2 Refactoring
//===============================================
/**
* Extra a single function to get Person url.
* Solution: Here we using R.compose.
* SO getURLFromPerson is point-free function.
*/ const generateUrl = (id) => `http://img.soicalnetwork.com/avatar/${id}.png`;
const getURLFromPerson = R.compose(
generateUrl,
R.propOr('default', 'id')
);
const getUpdatedPerson = (person) => R.assoc('avatar', getURLFromPerson(person), person);
const result = getUpdatedPerson(person);
console.log(result);
Here we use 'R.compose' to make 'getURLFromPerson' as a point-free function. Notice in the function, we no longer need to pass 'person' object as a param.
Third try:
//===============================================
// #3 Refactoring
//===============================================
/**
* In getUpdatedPerson function, we still relay on the 'person' param we pass in.
* We want to make it a point-free function also.
* Solution: we can use R.converge
*/
const generateUrl = (id) => `http://img.soicalnetwork.com/avatar/${id}.png`;
const getURLFromPerson = R.compose(
generateUrl,
R.propOr('default', 'id')
);
// const getUpdatedPerson = (person) => R.assoc('avatar', getURLFromPerson(person), person);
const getUpdatedPerson = R.converge(
R.assoc('avatar'),
[
getURLFromPerson,
R.identity
]
)
const result = getUpdatedPerson(person);
console.log(result);
The old verson of 'getUpdatedPerson' relay on 'person' param, to make it as point-free function style, we can use another way 'R.converge'.

[Ramda] Refactor to Point Free Functions with Ramda using compose and converge的更多相关文章
- [Ramda] Curry and Uncurry Functions with Ramda
Most of the functions offered by the ramda library are curried by default. Functions you've created ...
- [Ramda] Get Deeply Nested Properties Safely with Ramda's path and pathOr Functions
In this lesson we'll see how Ramda's path and pathOr functions can be used to safely access a deeply ...
- [Ramda] Convert Object Methods into Composable Functions with Ramda
In this lesson, we'll look at how we can use Ramda's invoker and constructNfunctions to take methods ...
- [Ramda] Refactor to a Point Free Function with Ramda's useWith Function
Naming things is hard and arguments in generic utility functions are no exception. Making functions ...
- [Ramda] Refactor a Promise Chain to Function Composition using Ramda
Promise chains can be a powerful way to handle a series of transformations to the results of an asyn ...
- [Ramda] Eliminate Function Arguments (Point-Free Style) with Ramda's Converge
When doing comparisons inside of functions, you end of relying heavily on the argument passed into t ...
- [Ramda] Count Words in a String with Ramda's countBy and invert
You can really unlock the power of ramda (and functional programming in general) when you combine fu ...
- [React] Update Component State in React With Ramda Lenses
In this lesson, we'll refactor a React component to use Ramda lenses to update our component state. ...
- [Ramda] Pick and Omit Properties from Objects Using Ramda
Sometimes you just need a subset of an object. In this lesson, we'll cover how you can accomplish th ...
随机推荐
- 【习题 7-9 UVA-1604】Cubic Eight-Puzzle
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] IDA* 保证这次移动的方格不和前一次重复. 然后加一个8数码的剪枝就行了. ->看看当前状态和目标状态有多少个地方是不一样的 ...
- 【Uva 11080】Place the Guards
[Link]: [Description] 一些城市,之间有道路相连,现在要安放警卫,警卫能看守到当前点周围的边,一条边只能有一个警卫看守,问是否有方案,如果有最少放几个警卫. [Solution] ...
- 【2017 Multi-University Training Contest - Team 6】Inversion
[链接]h在这里写链接 [题意] 给出一个序列,求2~n每一个数,下标不是这个数倍数的最大值是什么? [题解] 把a数组从大到小排序. 每个位置i,逆序枚举b数组,找到第一个对应下标不是i的倍数的,直 ...
- Detecting a return-oriented programming exploit
A method and apparatus for detecting a Return-Oriented Programming exploitation. At a computer devic ...
- 基于Redis bitmap实现开关配置功能
作者:zhanhailiang 日期:2014-12-21 bitmap api SETBIT key offset value 对key所储存的字符串值,设置或清除指定偏移量上的位(bit). 位的 ...
- 如何将String类型转换成任意基本类型
[原创][C#] 如何将String类型转换成任意基本类型 Posted on 2009-12-02 09:47 YCOE 阅读( 2843) 评论( 14) 编辑 收藏 前几天,在写一个自动 ...
- JSP中的EL表达式详细介绍
一.JSP EL语言定义 EL 提供了在 JSP 脚本编制元素范围外使用运行时表达式的功能.脚本编制元素是指页面中能够用于在 JSP 文件中嵌入 Java 代码的元素.它们通常用于对象操作以及执行那些 ...
- mycat快速体验(转)
横空出世的MyCat截至到2015年4月,保守估计已经有超过60个项目在使用,主要应用在电信领域.互联网项目,大部分是交易和管理系统,少量是信息系统.比较大的系统中,数据规模单表单月30亿.本人也初步 ...
- WCF REST (一)
最近工作中学习使用了WCF REST,REST 有很多好处 高效 简约 面向资源 而客户端调用 也变得非常简单.REST 入门的资料等 大家可以去网上找 这里主要分享下遇到的问题以及解决~ 一.环 ...
- 语言模型(Language Modeling)与统计语言模型
1. n-grams 统计语言模型研究的是一个单词序列出现的概率分布(probability distribution).例如对于英语,全体英文单词构成整个状态空间(state space). 边缘概 ...