[Compose] 9. Delay Evaluation with LazyBox
with Compose delay evaluation
2023-09-14 09:00:49 时间
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 + 1) .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 + 1) .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
相关文章
- vant安装报错npm WARN read-shrinkwrap This version of npm is compatible with lockfile
- ORA-01784: RECOVERABLE cannot be specified with database media recovery disabled ORACLE 报错 故障修复 远程处理
- ORA-25330: PL/SQL associative arrays may not be used with AQ array operations ORACLE 报错 故障修复 远程处理
- ORA-30078: partition bound must be TIME/TIMESTAMP WITH TIME ZONE literals ORACLE 报错 故障修复 远程处理
- ORA-38864: cannot create a guaranteed restore point with user specified SCN or time. ORACLE 报错 故障修复 远程处理
- ORA-39322: Cannot use transportable tablespace with TIMESTAMP WITH TIME ZONE columns and different time zone version. ORACLE 报错 故障修复 远程处理
- ORA-41705: conditions table cannot be dropped with DROP TABLE command ORACLE 报错 故障修复 远程处理
- MySQL Error number: 3978; Symbol: ER_FOREIGN_KEY_WITH_ATOMIC_CREATE_SELECT; SQLSTATE: HY000 报错 故障修复 远程处理
- ORA-01227: log string is inconsistent with other logs ORACLE 报错 故障修复 远程处理
- ORA-14047: ALTER TABLE|INDEX RENAME may not be combined with other operations ORACLE 报错 故障修复 远程处理
- SAP ABAP常见错误记录之一:In RFC modules, only parameters with pass by value are allowed (参考参数在更新任务中不允许)详解编程语言
- Tuning Linux Network Performance with Tun Devices(linuxtun设备)
- Link Oracle with Dream to Pursue Your Goals(oraclelink)
- Maximizing Productivity with Linux(linux改成英文)
- 分析学习Oracle深入理解使用With 分析(oracle使用with)
- Oracle中利用WITH子句的使用(oracle中的with)
- 语句使用Oracle两个WITH语句实现数据查询(oracle两个with)
- 从Oracle中挖掘洞见坚实的With表(oracle with表)
- 在js中使用"with"语句中跨frame的变量引用问题