博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Ramda] Refactor to Point Free Functions with Ramda using compose and converge
阅读量:4520 次
发布时间:2019-06-08

本文共 3121 字,大约阅读时间需要 10 分钟。

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: 1,    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'.

 

转载于:https://www.cnblogs.com/Answer1215/p/6402486.html

你可能感兴趣的文章
记录ok6410 jlink 命令行调试uboot
查看>>
ASP.net 内置对象
查看>>
QT使用mysql
查看>>
判断有无网
查看>>
ASP.NET简介
查看>>
php开发环境搭建
查看>>
select模型的原理、优点、缺点
查看>>
进程调度优先级
查看>>
HTML5表单那些事
查看>>
Spring MVC 学习总结(五)——校验与文件上传
查看>>
160505、oracle 修改字符集 修改为ZHS16GBK
查看>>
Spring 4 官方文档学习 Spring与Java EE技术的集成
查看>>
cocos+kbe问题记录
查看>>
自动化测试框架selenium+java+TestNG——配置篇
查看>>
测量标准体重
查看>>
(转)关于Android中为什么主线程不会因为Looper.loop()里的死循环卡死?引发的思考,事实可能不是一个 epoll 那么 简单。...
查看>>
SQL*Plus 系统变量之32 - NEWP[AGE]
查看>>
Spring配置文件总结
查看>>
4.三角形面积
查看>>
基础-事务
查看>>