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的更多相关文章

  1. [Ramda] Curry and Uncurry Functions with Ramda

    Most of the functions offered by the ramda library are curried by default. Functions you've created ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

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

  8. [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. ...

  9. [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 ...

随机推荐

  1. 【Educational Codeforces Round 35 B】Two Cakes

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 从小到大枚举x. 看看a/x+b/x是不是大于等于n 是的话. 看看是不是两种蛋糕都能凑一堆. 也即x的最大枚举量是min(a,b) ...

  2. [Javascript AST] 3. Continue: Write ESLint rule

    The rule we want to write is show warning if user using console method: // valid foo.console() conso ...

  3. Beginning iOS Programming

    Beginning iOS Programming 2014年 published by Wrox

  4. 转换PHP脚本成为windows的执行程序

    转换PHP脚本成为windows的执行程序 Convert a PHP script into a stand-alone windows executable I want to automate ...

  5. Android RxBus的实现及简单使用

    RxJava目前已经很火了,如果你尚未了解请看这里.对于RxJava这里不多做介绍.RxBus并不是一个库,而是一种模式.相信大多数开发者都使用过EventBus,作为事件总线通信库,如果你的项目已经 ...

  6. C# mongodb帮助类

    这是在C#连接MongoDB的帮助类,所使用的驱动是在Vs2015的Nuget管理器中下载的mongodb驱动. 下载第一个,会自动下载下面的两个,不要删除. 在配置文件中配置连接字符串connStr ...

  7. [TypeScript] Type check JavaScript files using JSDoc and Typescript 2.5

    Typescript 2.5 adds JSDoc type assertion support for javascript file via ts-check service. First of ...

  8. oracle 多行转多列查询

     oracle 多行转多列查询  ---create table Fruit(id int,name varchar(20), Q1 int, Q2 int, Q3 int, Q4 int);inse ...

  9. 素数表(Eratosthenes)

    怎么判断一个数是素数? 常规的方法是枚举从2开始的数,看看是否能被整除. 但是,如果要判断的数很多的时候,那么效率会十分低下.... 一个优化的方法是不用判断比这个数小的所有数(到平方根位置),而是判 ...

  10. 希捷硬盘扩容软件-----DiscWizard

    SeagateDiscWizard可为Seagate磁盘驱动器的使用提供便利.DiscWizard可帮助您迅速安装新的磁盘驱动器.并通过安装向导指导您在磁盘驱动器上完毕分区的创建和格式化. DiscW ...