[Compose] 16. Apply multiple functors as arguments to a function (Applicatives)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Compose] 16. Apply multiple functors as arguments to a function (Applicatives)相关的知识,希望对你有一定的参考价值。

?????????header   tip   size   cal   argument   cat   map   sel   screens   

We find a couple of DOM nodes that may or may not exist and run a calculation on the page height using applicatives.

 

For example we want to get the main content section size by reduce the height of header and footer, nomarlly we will do like this:

1. get the header height

2. get the footer height

3. Use screen height - header - footer.

 

const $ = selector =>
  Either.of({selector, height: 10})

const getScreenSize = (screen, header, footer) => screen - (header + footer);

$(???header???).chain(header => 
    $(???footer???).map(footer => 
        getScreenSize(800, header, footer)
    )   
)

 

This happens in sequential, we can use currey function to improve the code:

const getScreenSize = screen =>  header =>  footer => screen - (header + footer);
Either.of(getScreenSize)
    .ap($(???header???))
    .ap($(???footer???))

 

Or we can use:

const liftA2 = (f, fx, fy) =>
  fx.map(f).ap(fy)
const res = liftA2(getScreenSize(800), $(???header???), $(???footer???))

 

以上是关于[Compose] 16. Apply multiple functors as arguments to a function (Applicatives)的主要内容,如果未能解决你的问题,请参考以下文章

快速定位vscode log中的报错

ubuntu16.04安装docker-compose报错及解决

Ubuntu 16.04安装Docker-Compose

docker-compose Install Elasticsearch 7.16

一docker-compose部署elasticsearch+hanlp(7.16.2版本)----附完整镜像

多功能组合