[Compose] 9. Delay Evaluation with LazyBox
We rewrite the Box example using lazy evaulation.
Here is Box example:
const Box = (x) => ({
map: f => Box(f(x)),
fold: f => f(x)
});
const res = Box(' 64 ')
.map(abba => abba.trim())
.map(trimmed => new Number(trimmed))
.map(number => number + )
.map(x => String.fromCharCode(x))
.fold(x => x.toLowerCase());
console.log(res); // 'a'
So how to make it as Lazy Box? The Answer is instead of passing a value to the Box, we pass and function into it.
const LazyBox = (fn) => ({
map: g => LazyBox(() => g(fn())),
fold: g => g(fn()) // call the g()
});
const res = LazyBox(() => ' 64 ')
.map(abba => abba.trim())
.map(trimmed => new Number(trimmed))
.map(number => number + )
.map(x => String.fromCharCode(x))
.fold(x => x.toLowerCase());
console.log(res); // 'a'
inside map function, we use function defination:
() => g(fn())
Just defined, but not call. Using g() is to make it composeable.
When actually 'fold', we call fn():
fold: g => g(fn()) // call the g()
One important thing to take away, to make it lazy, wrap into a function
[Compose] 9. Delay Evaluation with LazyBox的更多相关文章
- Java性能提示(全)
http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...
- Swift的闭包(二):捕获值
闭包可以从定义它的上下文中捕获常量和变量. 在Swift中,捕获值最简单的例子是嵌套函数,举个例子: func makeIncrementer(forIncrement amount: Int) -& ...
- R2—《R in Nutshell》 读书笔记(连载)
R in Nutshell 前言 例子(nutshell包) 本书中的例子包括在nutshell的R包中,使用数据,需加载nutshell包 install.packages("nutshe ...
- Overview and Evaluation of Bluetooth Low Energy: An Emerging Low-Power Wireless Technology
转自:http://www.mdpi.com/1424-8220/12/9/11734/htm Sensors 2012, 12(9), 11734-11753; doi:10.3390/s12091 ...
- 【原创翻译】Reducing Branch Delay to Zero in Pipelined Processors
在流水线处理器中减少分支延迟到零 Antonio M. Gonzalez and Jose M. Llaberia 摘要 一种减少流水处理器中分支延迟到零的机制将在本文被描述以及评估.这种机制基于多重 ...
- Docker 小记 — Compose & Swarm
前言 任何相对完整的应用服务都不可能是由单一的程序来完成支持,计划使用 Docker 来部署的服务更是如此.大型服务需要进行拆分,形成微服务集群方能增强其稳定性和可维护性.本篇随笔将对 Docker ...
- Docker Compose 配置文件常用指令
Docker Compose 配置文件常用指令 YAML文件格式及编写注意事项 YAML是一种标记语言很直观的数据序列化格式,可读性高.类似于XML数据描述语言,语法比XML简单的很多. YAML数据 ...
- 附005.Docker Compose文件详解
一 Docker Compose文件简介 compose文件使用yml格式,主要分为了四个区域: version:用于指定当前docker-compose.yml语法遵循哪个版本 services:服 ...
- 基音检测算法的性能:Performance Evaluation of Pitch Detection Algorithms
http://access.feld.cvut.cz/view.php?cisloclanku=2009060001 Vydáno dne 02. 06. 2009 (15123 přečtení) ...
随机推荐
- Python并发编程-进程间数据共享
Manager中进程数据不安全 通过加锁解决 from multiprocessing import Manager,Process,Lock def main(dic,lock): lock.acq ...
- 详解Ubuntu Server下启动/停止/重启MySQL数据库的三种方式(ubuntu 16.04)
启动mysql: 方式一:sudo /etc/init.d/mysql start 方式二:sudo service mysql start 停止mysql: 方式一:sudo /etc/init.d ...
- Nearest Neighbor Search
## Nearest Neighbor Search ## Input file: standard input Output file: standard output Time limit: 1 ...
- 最小生成树---->prim算法的应用 hdu1863
畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- python3 django 安装
参考https://www.cnblogs.com/yuyang26/p/7411269.html 前提条件:python3.x环境 windows 步骤1 pip install Django==2 ...
- C和指针之学习笔记(1)
第1章 1.输入字符串 while((ch=getchar())!=EOF && ch!=’\n’) ; ch=getchar() while(ch!=EOF && ...
- 斐波那契数列(Java实现)
描述 一个斐波那契序列,F(0) = 0, F(1) = 1, F(n) = F(n-1) + F(n-2) (n>=2),根据n的值,计算斐波那契数F(n),其中0≤n≤1000. 输入 输入 ...
- 在活动之间切换(隐式Intent)
实验名称:在活动之间切换 实验现象:在主活动中点击button1可以进入下一个活动 使用技术:隐式Intent 步骤: 1.创建一个项目,加载布局并在布局中添加一个button 部分截图未截,直接Ne ...
- Python中进程无法结束的处理办法
1.方法一 http://hi.baidu.com/javalang/item/72fabf2359a30b464799625e 也就是说当线程使用start方法运行起来后,只有当run方法运行 ...
- Django 中文显示
Django中文显示: 1.如要在.py文件中显示中文,在文件首行加上:# -*- coding: utf-8 -*- 2.如要在html文件中显示中文,要将文件保存为UTF-8格式 3.在setti ...