首页 > 移动平台 > 详细

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

时间:2016-12-21 20:57:44      阅读:145      评论:0      收藏:0      [点我收藏+]

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)

原文:http://www.cnblogs.com/Answer1215/p/6208644.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!