[Compose] 9. Delay Evaluation with LazyBox

发布时间:2017-2-24 18:41:26 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"[Compose] 9. Delay Evaluation with LazyBox ",主要涉及到[Compose] 9. Delay Evaluation with LazyBox 方面的内容,对于[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 + 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()

上一篇:Web 项目中分享到微博、QQ空间等分享功能
下一篇:StackExchange.Redis 访问封装类

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。