[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'.
相关文章
- Welcom to Swift
- POJ 2039 To and Fro(模拟)
- [React Testing] Use React Testing Library to Render and Test Simple React Components
- [ES2019] Use JavaScript ES2019 flatMap to Map and Filter an Array
- [Tools] Convert SVG to a PDF in Node with PDFKit and SVG.js
- [Node.js] Write or Append to a File in Node.js with fs.writeFile and fs.writeFileSync
- [Vue-rx] Switch to a Function which Creates Observables with Vue.js and Rxjs
- [CSSinJS] Convert Sass (SCSS) Styled Button to CSSinJS with JavaScript Templates and Variables
- [Ramda] Refactor to Point Free Functions with Ramda using compose and converge
- [Functional Programming] Use the Callback and Closure Pattern to Build Advanced Async Behaviors [Full code]
- [Algorithm] 122. Best Time to Buy and Sell Stock II
- [ARIA] Add aria-expanded to add semantic value and styling
- [Node.js] Write or Append to a File in Node.js with fs.writeFile and fs.writeFileSync
- [Mobx] Use MobX actions to change and guard state
- [TypeScript] Configuring TypeScript Which Files to Compile with "Files" and "OutDir"
- [Flexbox] Using flex-direction to layout content horizontally and vertically
- [Py] Failed to import pydot. You must install pydot and graphviz for `pydotprint` to work
- 关于 SAP AMDP 调用错误消息 client-specific and restricts access to a client
- How to find all event handler list for a given event
- hierarchy change triggered in ERP and replicated to CRM
- Another way to define Angular controller
- centos7 nginx Failed to read PID from file /run/nginx.pid: Invalid argument 解决方法
- Up-to-date cache with EclipseLink and Oracle
- 3GPP TS 29244-g30 中英文对照 | 5.26.4 Reporting clock drift between TSN and 5GS times from UPF to SMF
- LeetCode 122 Best Time to Buy and Sell Stock II
- Leetcode:best_time_to_buy_and_sell_stock_II题解
- How to working with dates and times?