[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 ...
随机推荐
- Spring学习总结(5)——IOC注入方式总结
一.构造注入 在类被实例化的时候,它的构造方法被调用并且只能调用一次.所以它被用于类的初始化操作.<constructor-arg>是<bean>标签的子标签.通过其<v ...
- CSS3制作W3cplus的关注面板
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- SQLite-SQLiteDatabase 数据库实例练习
今天趁着有时间,自己在网上找了相关的数据库操作代码,进行了一下练习,先上代码 main.xml文件 <RelativeLayout xmlns:android="http://sche ...
- JavaScript篇(一)二叉树的插入 (附:可视化)
一.二叉树概念 二叉树(binary tree)是一颗树,其中每个节点都不能有多于两个的儿子. 字节一面,第一道就是二叉树的插入,在这里其实是对于一个二叉查找树的插入. 使二叉树成为二叉查找树的性质是 ...
- angular设置全局变量,可修改监听变量
创建service.module.ts import { NgModule, ModuleWithProviders } from '@angular/core'; import { SomeShar ...
- Mongodb总结5-通过装饰模式,用Mongodb解决Hbase的不稳定问题
最近继续学习Mongodb的根本原因,是为了解决今天的问题.项目中用到了Hbase,生产环境服务器用了3台,但是不够稳定,每2天左右,就连不上了.重启就好了,当然,这是一个历史遗留问题.我在想,是不是 ...
- 洛谷——P1046 陶陶摘苹果
https://www.luogu.org/problem/show?pid=1046 题目描述 陶陶家的院子里有一棵苹果树,每到秋天树上就会结出10个苹果.苹果成熟的时候,陶陶就会跑去摘苹果.陶陶有 ...
- Windows 7 下快速挂载和分离VHD文件的小脚本
1.保存以下代码为VDM.vbs,放在Windows\system32下 Dim ArgsSet Args = WScript.ArgumentsTranArgs = " "For ...
- (转)bat批处理的注释语句
在批处理中,段注释有一种比较常用的方法: goto start = 可以是多行文本,可以是命令 = 可以包含重定向符号和其他特殊字符 = 只要不包含 :start 这一行,就都是注释 :start 另 ...
- 【CS Round #48 (Div. 2 only)】Game of Chance
[链接]h在这里写链接 [题意] 在这里写题意 [题解] 在这里写题解 [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc++.h> using n ...