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'.

最新文章

  1. C#多线程学习
  2. Android ViewPager Fragment使用懒加载提升性能
  3. 在自己的框架中引用 PHPExcel
  4. hibernate对象关系实现(一)一对多
  5. Factory Girl使用
  6. [转]ubuntu server上网配置
  7. AIDL实例
  8. 为自己的Android应用添加广告
  9. poj 3624 Charm Bracelet(区间dp)
  10. 李航《统计学习方法》CH02
  11. javascript for循环 日期 select
  12. nc6 用业务插件注册来跑按钮事件
  13. tensorflow 之tf.nn.depthwise_conv2d and separable_conv2d实现及原理
  14. UVa 1149 装箱
  15. Linux Shell查看物理CPU个数、核数、逻辑CPU个数
  16. (1)json和pickle序列化模块
  17. 【转载】Vmware Vconverter从物理机迁移系统到虚拟机P2V
  18. Android filesystem system rw(read/write) permission
  19. 【BZOJ1449&&2895】球队预算 [费用流]
  20. DDR 复位

热门文章

  1. redirect_uri 参数错误
  2. 解决XCODE配置LLVM环境出现的问题
  3. Hibernate之API初识及增删改查实现
  4. Android RingtoneManager 铃声管理
  5. input输入框获得、失去焦点添加事件
  6. Python 极简教程(十一)字典 dict
  7. [python]bug和debug
  8. STL之vector容器的实现框架
  9. MySQL 概述和基础
  10. Docker---(1)Docker 简介