[Compose] 9. Delay Evaluation with LazyBox

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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()

 

以上是关于[Compose] 9. Delay Evaluation with LazyBox的主要内容,如果未能解决你的问题,请参考以下文章

9,docker基础之---Compose理论+部署word press

jquery-delay(),queue()

`docker-compose up`无效的服务名称'...' - 只允许[a-zA-Z0-9 ._ - ]字符

5.12流片delay复盘

docker-compose 构建失败,找不到文件但文件实际存在

Docker学习之docker-compose