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. 学习WWDC的好资源!

    学习WWDC的好资源. 大家都知道.要看Apple每年一度的WWDC,仅仅要到它的Developer站点去就能够了.那里有每年的研讨会视频,并且还能够下载每一个视频的SD或HD视频文件,以及相关的演示 ...

  2. js--07 编解码,eval

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  3. Qt之输出控制

    简述 在Qt项目开发过程中,往往需要对程序的一些信息进行控制,比如:打印日志.调试信息等,便于我们后期查找.跟踪及定位问题. 下面,我们来分享下常用的几种方式. 简述 示例代码 应用程序输出 控制台输 ...

  4. 提高IIS的FTP安全性 管理员的九阴真经

    提高IIS的FTP安全性 管理员的九阴真经 <九阴真经>是很多武林高手蒙昧以求的武林秘籍,在系统管理员这个武林中也有很多类似<九阴真经>一样的秘籍.在这里就向大家介绍一下有关提 ...

  5. 在Fedora 14 alpha 下测试Kvm情况(视频)

    今天刚忙完,便抽空做了fc14下的虚拟机的测试,总体感觉在2.6.35的kernel下启动和配置虚拟机的速度比原来要快乐许多,下面就带着各位体验一下吧.(硬件测试环境:IBM R52,2g内存,物理机 ...

  6. uiautomator——第一个例子:打开浏览器,输入网址

    1.在sdk安装目录:E:\Test_Tools\auto_test\app\adt-bundle-windows-x86-20131030\sdk\tools下启动uiautomatorviewer ...

  7. 记Bootstrap Table两种渲染方式

    这里主要区别两种Bootstrap Table的数据渲染方式,一.属性渲染方式,二.JS渲染方式 工作直接接手前人的,之前都直接在table标签上渲染属性,后面因为项目需要,同一页面的表格,需要申请不 ...

  8. IDFA和IMEI

    这里有一些解释: https://www.zhihu.com/question/38856446

  9. C#中数组与ArrayList的简单使用

    1. 多维数组 2. 锯齿数组 3. 数组的常用操作 4. ArrayList 1. 多维数组 多维数组:行数和列数在定义时已确定 string[,] arr = new string[2, 3]; ...

  10. Quick Sort Algorithm

    快速排序算法实现代码: //============================================================================ // Name : ...