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. spring+mybatis+Atomikos JTA事务配置说明

    一.概览 Atomikos是一个公司名字,旗下最著名的莫过于其Atomikos的事务管理器产品.产品分两个:一个是开源的TransactionEssentials,一个是商业的ExtremeTrans ...

  2. LinearLayout-layout_gravity 属性没有效果分析

    今天在一个布局文件中,遇到了一个问题,先看代码 <LinearLayout android:layout_width="match_parent" android:layou ...

  3. 10. Spring Boot JDBC 连接数据库

    转自:https://blog.csdn.net/catoop/article/details/50507516

  4. ACCESS数据库改名asp或asa

    到把mdb改为asp或asa ACCESS数据库:把数据库后缀名改成ASP是防止ACCESS数据库被下载

  5. Javascript和jquery事件--鼠标事件的小结

    1.鼠标事件的主要事件应该是mouseup, mousedown, mousewheel, mousemove, mouseover, moveout. <1>其中mouseup和mous ...

  6. ehcache、memcache、redis三大缓存比较(转)

    最近项目组有用到这三个缓存,去各自的官方看了下,觉得还真的各有千秋!今天特意归纳下各个缓存的优缺点,仅供参考!  Ehcache 在Java项目广泛的使用.它是一个开源的.设计于提高在数据从RDBMS ...

  7. BigBoss按键映射

    // BigBoss: SBSettings Toggle Spec 按键映射 http://thebigboss.org/guides-iphone-ipod-ipad/sbsettings-tog ...

  8. 使用wepy开发微信小程序商城第二篇:路由配置和页面结构

    使用wepy开发微信小程序商城 第二篇:路由配置和页面结构 前言: 最近公司在做一个微信小程序的项目,用的是类似于vue的wepy框架.我也借此机会学习和实践一下. 小程序官方文档:https://d ...

  9. 10.5 android输入系统_Reader线程_使用EventHub读取事件和核心类及配置文件_实验_分析

    4. Reader线程_使用EventHub读取事件 使用inotify监测/dev/input下文件的创建和删除 使用epoll监测有无数据上报 细节: a.fd1 = inotify_init(& ...

  10. iOS开发UI篇--一个侧滑菜单SlidingMenu

    一.简介 侧滑菜单已经成为app一个极常用的设计,不管是事务类,效率类还是生活类app.侧滑菜单因Path 2.0和Facebook为开发者熟知,国内目前也有很多流行app用到了侧滑菜单,比如QQ.网 ...