[JS Compose] 1. Refactor imperative code to a single composed expression using Box
After understanding how Box is, then we are going to see how to use Box to refacotr code, to un-nested expression.
For example, we have code:
const moneyToFloat = str => {
const cost = str.replace(/\$/g, '');
return parseFloat(cost);
} const percentToFloat = str => {
const cost = parseFloat(str.replace(/\$/g, ''));return cost * 0.01;
} const applyDiscount = (price, prec) => {
const cost = moneyToFloat(price);
const p = percentToFloat(prec);
return cost - cost * p;
} const result = applyDiscount('$5.00', '20%')
console.log(result) //
So how it would be when we using Box version:
const moneyToFloat = str => {
const cost = str.replace(/\$/g, '');
return parseFloat(cost);
} const moneyToFloat = str =>
Box(str)
.map(s => s.replace(/\$/g, ''))
.map(s => parseFloat(cost))
Well, nothing really impress here, we put 'str' into Box, and using map to do all the logic.
const percentToFloat = str => {
const cost = parseFloat(str.replace(/\$/g, ''));
return cost * 0.01;
} const percentToFloat = str =>
Box(str.replace(/\$/g, ''))
.map(s => parseFloat(s))
.map(s => s * 0.01)
This part, notice we un-nest 'parseFloat(str.replace(...))', make logic more readable by using map.
const applyDiscount = (price, prec) => {
const cost = moneyToFloat(price);
const p = percentToFloat(prec);
return cost - cost * p;
} const applyDiscount = (price, prec) =>
moneyToFloat(price)
.fold(cost => percentToFloat(prec)
.fold(p => cost - cost * p))
Notice here, because 'moneyToFloat' return a Box, so we are able to chain map on that.
And 'cost', we can use clouse to remember 'cost', and chain 'fold' on precentToFloat. The reason we use 'fold' instead of 'map', is 'fold' we can get value out of Box.
const Box = x =>
({
map: f => Box(f(x)),
fold: f => f(x),
toString: () => `Box(${x})`
}) const moneyToFloat = str =>
Box(str)
.map(s => s.replace(/\$/g, ''))
.map(s => parseFloat(cost)); const percentToFloat = str =>
Box(str.replace(/\$/g, ''))
.map(s => parseFloat(s))
.map(s => s * 0.01); const applyDiscount = (price, prec) =>
moneyToFloat(price)
.fold(cost => percentToFloat(prec)
.fold(p => cost - cost * p)) const result = applyDiscount('$5.00', '20%')
console.log(result) //
[JS Compose] 1. Refactor imperative code to a single composed expression using Box的更多相关文章
- [JS Compose] 3. Use chain for composable error handling with nested Eithers (flatMap)
We refactor a function that uses try/catch to a single composed expression using Either. We then int ...
- [转]Node.js tutorial in Visual Studio Code
本文转自:https://code.visualstudio.com/docs/nodejs/nodejs-tutorial Node.js tutorial in Visual Studio Cod ...
- myeclipse中导入js报如下错误Syntax error on token "Invalid Regular Expression Options", no accurate correc
今天在使用bootstrap的时候引入的js文件出现错误Syntax error on token "Invalid Regular Expression Options", no ...
- [JS Compose] 2. Enforce a null check with composable code branching using Either
We define the Either type and see how it works. Then try it out to enforce a null check and branch o ...
- [JS Compse] 4. A collection of Either examples compared to imperative code
For if..else: const showPage() { if(current_user) { return renderPage(current_user); } else { return ...
- 【JS】Advanced1:Object-Oriented Code
Object-Oriented Code 1. var Person = function (name) { this.name = name; }; Person.prototype.say = f ...
- Node.js and Forever “exited with code: 0”
CentOs 6.5 using root acount, I have a working Node.js Express app: root@vps [/home/test/node]# npm ...
- [Functional Programming Monad] Refactor Stateful Code To Use A State Monad
When we start to accumulate functions that all work on a given datatype, we end up creating a bunch ...
- [JS Compose] 0. Understand 'Box' or 'Container', they are just like Array!
We'll examine how to unnest function calls, capture assignment, and create a linear data flow with a ...
随机推荐
- Javascript:存储和读取cookie
Cookie是网页开发中的一项重要技术,用于在本地存储一些信息(如username,password.登录状态)以便用户下一次訪问时使用(或在其他页面使用). cookie的格式是键值对,多个键值对之 ...
- vue.js最最最最简单实例
vue.js最最最最简单实例 一.总结 一句话总结: 1.vue.js实现实现数据的双向绑定用的是什么标记? 双大括号:比如{{message}} 2.vue数据循环输出的标记是什么? 用的是标签的v ...
- codevs 5960 信使
codevs 5960 信使 题目描述 Description 战争时期,前线有n个哨所,每个哨所可能会与其他若干个哨所之间有通信联系.信使负责在哨所之间传递信息,当然,这是要花费一定时间的(以天为单 ...
- 【hdu 6181】Two Paths
[链接]http://acm.hdu.edu.cn/showproblem.php?pid=6181 [题意] 让你求从1到n的次短路 [题解] 模板题; 因为点可以重复走; 则一定会有次短路. di ...
- C# winform利用反射和自定义特性加载功能模块(插件式开发)
由于在实际的工作中, 碰见这样的一个问题: 一个软件, 销售给A客户 他需要所有功能, 但是销售给B客户, 他只需要其中的一部分, 1.如果我们在实际的开发过程中, 没有把一些功能模块区分开来的话, ...
- [React & Testing] Simulate Event testing
Here we want to test a toggle button component, when the button was click, state should change, styl ...
- theme-windowAnimationStyle 动画设置
对于windowAnimationStyle 的引用,目前自己发现的有两处 1.就是直接在Theme 中引用的,如下 <style name="Theme.Funui" pa ...
- 18. springboot整合jsp
转自:https://blog.csdn.net/u012562943/article/details/51836729
- 给已有数据的oracle表建立外键关系
PS:这里是给自己做个备忘,下次遇到同类问题的时候,方便查找: 客户在有主外键关系的2张表进行页面删除时报错已有子记录,运维后台处理的时候应该找出相应的数据,先删除子记录,在删主表记录:但客户要的急, ...
- SQLite基础学习
SQLite是一款轻量级数据库,集成于android中,以下从分享一下自己学习的. 在查阅资料时有一些好的说明就直接用了: 主要的curd语句 以下SQL语句获取5条记录,跳过前面3条记录 selec ...