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

 

posted @   Zhentiw  阅读(341)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2016-02-15 [Regular Expressions] Find a String that Precedes Another String ?= , ?!
2016-02-15 [Regular Expressions] Find Groups of Characters, and ?:
点击右上角即可分享
微信分享提示