码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2016-12-21 21:00:05      阅读:251      评论:0      收藏:0      [点我收藏+]

标签: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)

标签:header   tip   size   cal   argument   cat   map   sel   screens   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!