We'll examine how to unnest function calls, capture assignment, and create a linear data flow with a type we call Box. This is our introduction to working with the various container-style types.

At first, might not be comforable with 'Box' or 'Container' idea. It bit similar to 'Array'.
Lets see an example first:
const nextCharForNumberString = str =>
[str] // [' 64 ']
.map(s => s.trim()) // ['64']
.map(r => parseInt(r)) // [64]
.map(i => i + ) // [65]
.map(i => String.fromCharCode(i)) // ["A"]
.map(c => c.toLowerCase()) // ["a"] const result = nextCharForNumberString(' 64 ') console.log(result) // ["a"]

Notice that, the function take one param 'str', inside the function body, the first thing we do is put 'str' into Array. Well, we know [] this is Array because we know Javascript, we know programming. Lets assume we don't know programming, '[]' this is just something looks like a 'BOX'!

So what we do is, put 'str' into 'BOX', and every single step, we use 'map' to do transform.  Wait a second here... Because we know programming, we know what 'map' acutally does. So we think ok, this is how it should be.

Assume again, we don't know programming... what will you describe what happen then?

Well, I saw ' 64 ' inside a 'BOX'; then '64' inside the BOX; then 64 inside the BOX; then 65....

One thing we have to notice here now is that, 'BOX' is always there! So what actually 'map' does? Try to describe it again...

Well.. Uhm... what map actually does is that it goes into a BOX, according to the logic passed to the map and its value, doing transform to the value, then update value and set to a new BOX. So next opreation get this new BOX and new value.

So now you can see, Array just like a Box, so we can replace to:

const nextCharForNumberString = str =>
Box(str) // Box(' 64 ')
.map(s => s.trim()) // Box('64')
.map(r => parseInt(r)) // Box(64)
.map(i => i + ) // Box(65)
.map(i => String.fromCharCode(i)) // Box("A")
.map(c => c.toLowerCase()) // Box("a") const result = nextCharForNumberString(' 64 ') console.log(result) // Box("a")

Of course, this code won't work in Javascript.

So we need to define 'Box': it should be a function return an object, which has 'map' function, 'map' function accpect an function as param and take the value pass from the Box. The return value from 'map' should still put back into the Box, so that, the next map function can use it.

const Box = (x) => ({
map: f => Box(f(x))
})

So far is good, but we don't want the result as 'Box("a")', we just want the value "a", so we can add another function into map, which call 'fold', the function will just return the updated value.

const Box = (x) => ({
map: f => Box(f(x)),
fold: f => f(x)
}) const nextCharForNumberString = str =>
Box(str) // Box(' 64 ')
.map(s => s.trim()) // Box('64')
.map(r => parseInt(r)) // Box(64)
.map(i => i + ) // Box(65)
.map(i => String.fromCharCode(i)) // Box("A")
.fold(c => c.toLowerCase()) // "a" const result = nextCharForNumberString(' 64 ') console.log(result) // "a"

Now we got "a".

The important thing to take away from here is: 'Box' and 'Container' are nothing but like 'Array' which we already know.

'map' function is not just loop over Array, it is actually goes into the Box and update the value, and return a new Box with the updated value.

[JS Compose] 0. Understand 'Box' or 'Container', they are just like Array!的更多相关文章

  1. 窥探Vue.js 2.0

    title: 窥探Vue.js2.0 date: 2016-09-27 10:22:34 tags: vue category: 技术总结 --- 窥探Vue.js2.0 令人兴奋的Vue.js 2. ...

  2. 【JS】heatmap.js v1.0 到 v2.0,详细总结一下:)

    前段时间,项目要开发热力图插件,研究了heatmap.js,打算好好总结一下. 本文主要有以下几部分内容: 部分源码理解 如何迁移到v2.0 v2.0官方文档译文 关于heatmap.js介绍,请看这 ...

  3. Heatmap.js v2.0 – 最强大的 Web 动态热图

    Heatmap 是用来呈现一定区域内的统计度量,最常见的网站访问热力图就是以特殊高亮的形式显示访客热衷的页面区域和访客所在的地理区域的图示.Heatmap.js 这个 JavaScript 库可以实现 ...

  4. 【翻译】Ext JS 5.0.1 中的新功能

    原文:What's New in Ext JS 5.0.1 今天,我们很高兴的宣布Ext JS 5.0.1发布了!此维护版本基于Sencha社区的反馈做了一些改进.下面让我们来了解一下这些改变. 可访 ...

  5. 使用 Vue.js 2.0+ Vue-resource 模仿百度搜索框

    使用 Vue.js 2.0 模仿百度搜索框 <!DOCTYPE html> <html> <head> <meta charset="utf-8&q ...

  6. 窥探Vue.js 2.0 - Virtual DOM到底是个什么鬼?

    引言 你可能听说在Vue.js 2.0已经发布,并且在其中新添加如了一些新功能.其中一个功能就是"Virtual DOM". Virtual DOM是什么 在之前,React和Em ...

  7. CentOS 6 中安装Node.js 4.0 版本或以上

    如果想在CentOS 6 中安装Node.js >4.0,如果通过以往的方式安装: wget http://nodejs.org/dist/v4.0.0/node-v4.0.0.tar.gz t ...

  8. swipe.js 2.0 轻量级框架实现mobile web 左右滑动

    属性总结笔记如下: <style> .swipe { overflow: hidden; //隐藏溢出 清楚浮动 visibility: hidden; //规定元素不可见 (可以设置,当 ...

  9. Vue.js 2.0 参考手册.CHM下载

    下载地址 Vue.js 2.0 参考手册.CHM下载链接: http://pan.baidu.com/s/1kVbhd4b 密码: wxfh

随机推荐

  1. hdu 5098 双队列拓扑排序

    http://acm.hdu.edu.cn/showproblem.php?pid=5098 软件在安装之后需要重启才能发挥作用,现在给你一堆软件(有的需要重启有的不需要)以及安装这个软件之前需要哪些 ...

  2. 《Automatic Face Classification of Cushing’s Syndrome in Women – A Novel Screening Approach》学习笔记

    <针对女性库欣综合征患者的自动面部分类-一种新颖的筛查方法> Abstract 目的:库兴氏综合征对身体造成相当大的伤害如果不及时治疗,还经常是诊断的时间太长.在这项研究中,我们旨在测试面 ...

  3. 工大助手--C#之DataGrid

    工大助手 基本内容 工大助手(桌面版) 实现登陆.查成绩.计算加权平均分等功能 团队人员 13070002 张雨帆 13070003 张帆 13070004 崔巍 13070006 王奈 130700 ...

  4. 第十二章:Android数据存储(下)

    一.SQLite介绍 提到数据存储问题,数据库是不得不提的.数据库是用来存储关系型数据的不二利器.Android为开发者提供了强大的数据库支持,可以用来轻松地构造基于数据库的应用.Android的数据 ...

  5. eclipse导入Android项目后,项目的名称变为了主Activity的名称

    不要使用File -> Import -> Android -> Existing Android Code into Workspace,而是用 File -> Import ...

  6. python __setattr__, __getattr__, __delattr__, __call__

    python __setattr__, __getattr__, __delattr__, __call__ getattr `getattr`函数属于内建函数,可以通过函数名称获取 value = ...

  7. [JS9] document's bgColor改变背景颜色

    <HTML> <HEAD> <TITLE>设置背景颜色</TITLE> </HEAD> <BODY> <CENTER> ...

  8. pasteimg浏览器中粘贴图片jQuery插件

    pasteimg是一款可以在浏览器中实现图片粘贴的jQuery插件,兼容Chrome.Firefox.IE11以及其他使用这些内核的浏览器,比如,国内著名的360浏览器. pasteimg可以识别浏览 ...

  9. AngularJS快速入门指南01:导言

    AngularJS使用新的attributes扩展了HTML AngularJS对单页面应用的支持非常好(SPAs) AngularJS非常容易学习 现在就开始学习AngularJS吧! 关于本指南 ...

  10. Hibernate各种主键生成器策略与配置详解(转载)

    http://www.cnblogs.com/kakafra/archive/2012/09/16/2687569.html 1.assigned 主键由外部程序负责生成,在 save() 之前必须指 ...