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. halcon的算子列表

    Chapter 1 :Classification 1.1 Gaussian-Mixture-Models 1.add_sample_class_gmm 功能:把一个训练样本添加到一个高斯混合模型的训 ...

  2. 无线安全专题_破解篇02--kali破解pin码

    最近项目有点紧,所以本应该上周发的文章,拖到了本周三,在此说声抱歉.无线安全专题,我打算系统地写六个部分,分别为破解篇,攻击篇,欺骗篇,路由篇,移动篇和蓝牙篇,当然在发布的过程中,可能还会掺杂着发布f ...

  3. APUE1

    [APUE]进程控制(上)   一.进程标识 进程ID 0是调度进程,常常被称为交换进程(swapper).该进程并不执行任何磁盘上的程序--它是内核的一部分,因此也被称为系统进程.进程ID 1是in ...

  4. 让Sqlite脱离VC++ Runtime独立运行

    前段时间在开发OrayTalk(傲瑞通)的聊天记录模块时用到了Sqlite,这是我第一次接触和使用Sqlite,总体感觉还是非常不错的.这里把我使用Sqlite的经验跟大家分享一下. 一.关于Sqli ...

  5. 设计模式之美:Bridge(桥接)

    索引 别名 意图 结构 参与者 适用性 效果 相关模式 实现 实现方式(一):使用 Bridge 模式分离抽象部分和实现部分. 别名 Handle Body 意图 将抽象部分与它的实现部分分离,使它们 ...

  6. Try..Finally..相信自己的眼睛

    问题提出 try { return x; } finally { x = null; } 上面这段代码到底怎么执行的? try..catch..finally 介绍 在MSDN中,try..catch ...

  7. C语言 数组 列优先 实现

    C语言数组结构列优先顺序存储的实现 (GCC编译). 从行优先转换为列优先存储方式, 与行优先相比, 不同之处在于改变了数组维界基址的先后顺序, 从而改变了映像函数常量基址. /** * @brief ...

  8. Android开发学习总结(六)—— APK反编译

    学习和开发Android应用有一段时间了,今天写一篇博客总结一下Android的apk文件反编译.我们知道,Android应用开发完成之后,我们最终都会将应用打包成一个apk文件,然后让用户通过手机或 ...

  9. [OpenGL] 1、环境搭建及最小系统

    >_<: 首先推荐一个企业版的VC6.0自带OpenGL和DirectX,非常方便:http://pan.baidu.com/s/1mgIAGi8 PS: 要注意这里的OpenGL建立的工 ...

  10. OS——进程简答题(1)

    1,叙述进程和程序的主要区别. 解:进程和程序是两个既有联系又有区别的两个概念,它们的主要区别如下: (1)程序是指令的有序集合,其本身没有任何运行的含义,它是一个静态的概念.而进程是程序在处理机上的 ...