[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 type we call Box. This is our introduction to working with the various container-style types.
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!的更多相关文章
- 窥探Vue.js 2.0
title: 窥探Vue.js2.0 date: 2016-09-27 10:22:34 tags: vue category: 技术总结 --- 窥探Vue.js2.0 令人兴奋的Vue.js 2. ...
- 【JS】heatmap.js v1.0 到 v2.0,详细总结一下:)
前段时间,项目要开发热力图插件,研究了heatmap.js,打算好好总结一下. 本文主要有以下几部分内容: 部分源码理解 如何迁移到v2.0 v2.0官方文档译文 关于heatmap.js介绍,请看这 ...
- Heatmap.js v2.0 – 最强大的 Web 动态热图
Heatmap 是用来呈现一定区域内的统计度量,最常见的网站访问热力图就是以特殊高亮的形式显示访客热衷的页面区域和访客所在的地理区域的图示.Heatmap.js 这个 JavaScript 库可以实现 ...
- 【翻译】Ext JS 5.0.1 中的新功能
原文:What's New in Ext JS 5.0.1 今天,我们很高兴的宣布Ext JS 5.0.1发布了!此维护版本基于Sencha社区的反馈做了一些改进.下面让我们来了解一下这些改变. 可访 ...
- 使用 Vue.js 2.0+ Vue-resource 模仿百度搜索框
使用 Vue.js 2.0 模仿百度搜索框 <!DOCTYPE html> <html> <head> <meta charset="utf-8&q ...
- 窥探Vue.js 2.0 - Virtual DOM到底是个什么鬼?
引言 你可能听说在Vue.js 2.0已经发布,并且在其中新添加如了一些新功能.其中一个功能就是"Virtual DOM". Virtual DOM是什么 在之前,React和Em ...
- 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 ...
- swipe.js 2.0 轻量级框架实现mobile web 左右滑动
属性总结笔记如下: <style> .swipe { overflow: hidden; //隐藏溢出 清楚浮动 visibility: hidden; //规定元素不可见 (可以设置,当 ...
- Vue.js 2.0 参考手册.CHM下载
下载地址 Vue.js 2.0 参考手册.CHM下载链接: http://pan.baidu.com/s/1kVbhd4b 密码: wxfh
随机推荐
- Linux内核--网络栈实现分析(一)--网络栈初始化
本文分析基于内核Linux Kernel 1.2.13 原创作品,转载请标明http://blog.csdn.net/yming0221/article/details/7488828 更多请看专栏, ...
- AIX之ASM存储扩容
ASM存储扩容操作其实很简单,无非就是向DiskGroup(简称DG)里添加物理磁盘,增加DG的存储空间.说来简单,其实操作过程中有很多小细节要注意,否则,带来的后果是灾难性的. ASM扩容操作步骤( ...
- Docker学习笔记整理
Docker接触有一段时间了,但是对于Docker的使用可以说是一点不会.现在要在Docker上部署基于Angular开发的页面.只能一点点积累查找的资料,顺手整理一下,方便后面的回顾. 其中用到的资 ...
- [异常] MyEclipse Deploy点不开 An internal error occurred during: "Launching MVC on Tomcat 6.x". java.lang.NullPointerException
>_<" 今天做struts时,工程写好了,可是点击MyEclipse上面的Delopy不会跳出常规的窗口,导致各种坑~ >_<" 如果直接运行还出现下面的 ...
- [汇编] 从键盘输入一个一位数字,然后响铃n声
; multi-segment executable file template. data segment ends stack segment dw dup() ends code segment ...
- Linux:Vim
模式介绍: Vim具备6种基本模式和5中派生模式. 普通模式 启动后的默认模式,用于:移动光标.删除文本等待,常用命令: dd:删除当前行. [number]dd:连续执行number对应次数的dd命 ...
- jenkins2 Jenkinsfile
推荐使用Jenkinsfile代替将groovy脚本直接写在jenkins job里. 文章来自:http://www.ciandcd.com文中的代码来自可以从github下载: https://g ...
- 彻底掌握this,call,apply
说起js里面比较头疼的知识点,this的指向,call与apply的理解这三者肯定算的上前几,好的js开发者这是必须迈过的槛.今天就总结下这三者紧密相连的关系. 首先推荐理解call的用法 Funct ...
- maven nexus 3 third party 构件上传
mvn -e deploy:deploy-file -DgroupId=com.oracle -DartifactId=JDBCDriver -Dversion=12.0.1 -Dpackaging= ...
- [读书笔记]C#学习笔记七: C#4.0中微小改动-可选参数,泛型的可变性
前言 下面就开始总结C#4.0的一些变化了, 也是这本书中最后的一点内容了, 这一部分终于要更新完了. 同时感觉再来读第二遍也有不一样的收获. 今天很嗨的是武汉下雪了,明天周六,一切都是这么美好.哈哈 ...