首页 > 代码库 > [Compose] 17. List comprehensions with Applicative Functors
[Compose] 17. List comprehensions with Applicative Functors
We annihilate the need for the ol‘ nested for
loop using Applicatives.
For example we have this kind of nested loop code:
for(x in xs){ for(x in ys){ for(z in zs){ } }}
We can refactor it by using List comprehension:
const {List} = Immutable;
const res1 = List.of(x => x).ap(List([1,2,3]));console.log(res1) // List [ 1,2,3 ]
const res1 = List.of(x => y=> `${x} - ${y}`) .ap(List([‘teeshirt‘, ‘sweater‘])) .ap(List([‘large‘, ‘medium‘, ‘small‘]));console.log(res1) //List [ "teeshirt - large", "teeshirt - medium", "teeshirt - small", "sweater - large", "sweater - medium", "sweater - small" ]
[Compose] 17. List comprehensions with Applicative Functors
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。