[Compose] 9. Delay Evaluation with LazyBox
2024-08-28 02:13:58
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
最新文章
- Tomcat调优及JMX监控
- js017-错误处理与调试
- Netbeans 设置模板
- [SoapUI] SoapUI JDBC REST 连接 Netezza
- 简单的javascript--test2
- 2015ACM/ICPC亚洲区长春站 H hdu 5534 Partial Tree
- 【新产品发布】【GK101 10MHz任意波发生器】
- ASP.NET MVC5 第4章
- mapreduce学习指导及疑难解惑汇总
- Msys+MinGW编译VLC
- jquery中的globalEval()源码分析
- spring jdbcTemplate 事务,各种诡异,包你醍醐灌顶!
- 进入jsp页面的6种方法
- HDU 5961 传递 随机化
- 安装phpssdbadmin
- Robotics Tools
- easyui---修改删除查询
- Spring Boot切换为APR模式
- SpringMVC拦截器实现:当用户访问网站资源时,监听session是否过期
- 【IntelliJ IDEA】idea上提交代码到GitHub,已经提交了 但是GitHub上却没有的解决办法