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的更多相关文章

  1. [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 ...

  2. [转]Node.js tutorial in Visual Studio Code

    本文转自:https://code.visualstudio.com/docs/nodejs/nodejs-tutorial Node.js tutorial in Visual Studio Cod ...

  3. myeclipse中导入js报如下错误Syntax error on token "Invalid Regular Expression Options", no accurate correc

    今天在使用bootstrap的时候引入的js文件出现错误Syntax error on token "Invalid Regular Expression Options", no ...

  4. [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 ...

  5. [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 ...

  6. 【JS】Advanced1:Object-Oriented Code

    Object-Oriented Code 1. var Person = function (name) { this.name = name; }; Person.prototype.say = f ...

  7. 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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 公告:本博客搬迁到:http://www.courtier.cc

    公告:       您好,本人意见本博客搬迁到:http://www.courtier.cc

  2. [NowCoder]牛客OI周赛1 题解

    A.分组 首先,认识的人不超过3个,因此不存在无解的方案 考虑直接构造,先把所有点设为1,顺序扫一遍把有问题的点加入队列 每次取队头,将其颜色取反,再更新有问题的点 复杂度:考虑到每个点不会操作2次, ...

  3. 记号(notation)的学习

    数学的记号(notation) 记号具体代表什么含义,取决于你的定义: 比如这样的 d⃗  一个向量,每个分量 d(i) 表示的是从初始结点 v 到当前节点 vi 的最短路径:也即这样的一个向量的每一 ...

  4. gomail发送附件

    采用github.com/go-gomail/gomail/ 的邮件功能,可以发送附件 以及html文档,下面是其给出的demo,测试通过. package main //cmd: go get go ...

  5. HDU4825 Xor Sum(贪心+Trie树)

    Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeu ...

  6. MariaDB 安装 (YUM)

    在CentOS 7.0安装MariaDB的数据库,在这里记录下安装过程,以便以后查看. 1. 安装MariaDB 安装命令 yum -y install mariadb mariadb-server ...

  7. React 和 Vue 对比

    React 和 Vue 有许多相似之处,它们都有:   * 使用 Virtual DOM * 提供了响应式 (Reactive) 和组件化 (Composable) 的视图组件. * 将注意力集中保持 ...

  8. 【Energy Forecasting】能源预測的发展和展望

    说明 本文的内容来自Tao Hong博士的Energy Forecasting: Past, Present and Future一文的翻译和整理. 引入 能源预測包括了电力行业中有关预測的广泛的内容 ...

  9. ASM(四) 利用Method 组件动态注入方法逻辑

    这篇继续结合样例来深入了解下Method组件动态变更方法字节码的实现.通过前面一篇,知道ClassVisitor 的visitMethod()方法能够返回一个MethodVisitor的实例. 那么我 ...

  10. elasticsearch选举master

    数据节点确定了master以后.会开启MasterPinger线程来与其保持心跳通信的逻辑假设通信过程中链接断开,会通过MasterNodeFailureListener监听器进行回调处理处理过程中会 ...