首页 > 代码库 > [Compose] 16. Apply multiple functors as arguments to a function (Applicatives)
[Compose] 16. Apply multiple functors as arguments to a function (Applicatives)
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)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。