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([,,]));
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的更多相关文章

  1. [Compose] 15. Applicative Functors for multiple arguments

    Working our way backwards from solution to problem, we define an applicative functor, then use it to ...

  2. [Functional Programming] Working with two functors(Applicative Functors)-- Part1 --.ap

    What is applicative functor: the ability to apply functors to each other. For example we have tow fu ...

  3. [Functional Programming] Working with two functors(Applicative Functors)-- Part2 --liftAN

    Let's examine a pointfree way to write these applicative calls. Since we know map is equal to of/ap, ...

  4. <STL源码剖析> 6.3.6 power

    计算power的算法说明 http://www.sxt.cn/u/324/blog/2112 翻译自  http://videlalvaro.github.io/2014/03/the-power-a ...

  5. Haskell学习-functor

    原文地址:Haskell学习-functor 什么是Functor functor 就是可以执行map操作的对象,functor就像是附加了语义的表达式,可以用盒子进行比喻.functor 的定义可以 ...

  6. Function Composition vs Object Composition

    In functional programming, we create large functions by composing small functions; in object-oriente ...

  7. [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 usi ...

  8. 浅释Functor、Applicative与Monad

    引言 转入Scala一段时间以来,理解Functor.Applicative和Monad等概念,一直是我感到头疼的部分.虽然读过<Functors, Applicatives, And Mona ...

  9. Docker之Compose服务编排

    Compose是Docker的服务编排工具,主要用来构建基于Docker的复杂应用,Compose 通过一个配置文件来管理多个Docker容器,非常适合组合使用多个容器进行开发的场景. 说明:Comp ...

随机推荐

  1. angular4 监听input框输入值的改变

    angular中一般控件会有change事件,但是如果某些控件没有这个事件 我们如何监听值的变化呢? 对于双向绑定的值,当值改变后监听事件有如下写法: 1. 如果是ngModel可以用ngModelC ...

  2. 洛谷——P4017 最大食物链计数

    P4017 最大食物链计数 题目背景 你知道食物链吗?Delia生物考试的时候,数食物链条数的题目全都错了,因为她总是重复数了几条或漏掉了几条.于是她来就来求助你,然而你也不会啊!写一个程序来帮帮她吧 ...

  3. 对Java Serializable(序列化)的理解和总结(一)

    导读:最近在做项目的过程中,发现一个问题,就是我们最开始的时候,传递参数包括返回类型,都有map类型.但是由于map每次都要匹配key值,很麻烦.所以在之后就将参数传递和返回类型全都改成了实体bean ...

  4. l1和l2正则化的区别 - 面试错题集

    L0:计算非零个数,用于产生稀疏性,但是在实际研究中很少用,因为L0范数很难优化求解,是一个NP-hard问题,因此更多情况下我们是使用L1范数L1:计算绝对值之和,用以产生稀疏性,因为它是L0范式的 ...

  5. USACO 2017 FEB Gold visitfj 最短路

    题意 有一幅n*n的方格图,n <= 100,每个点上有一个值.从(1,1)出发,走到(n,n),只能走四联通.每走一步花费t,每走三步需要花费走完三步后到达格子的值.求最小花费的值. 拆点,d ...

  6. asp.net调用存储过程1

    1,传入参数,传出参数 public int GetTeam1Id(string userId)        {            int team1ID = -1;            st ...

  7. HashMap结构及使用

    HashMap的数据结构 HashMap主要是用数组来存储数据的,我们都知道它会对key进行哈希运算,哈系运算会有重复的哈希值,对于哈希值的冲突,HashMap采用链表来解决的.在HashMap里有这 ...

  8. SQl CASE 语句的嵌套使用方式

    case具有两种格式.简单case函数和case搜索函数.  1.简单case函数 case sex when ’1’ then ’男’ when ’2’ then ’女’else ’其他’ end ...

  9. PHP str_pad() 函数

    str_pad() 函数把字符串填充为指定的长度. 进入 详细介绍页面

  10. 三分钟教你学Git (四)之紧急救助

    假设你不小心git reset --hard HEAD^ 然后这个commit又没有在别的git仓库中,怎么办?是不是这次改动就丢了呢? 当然不是,git为我们每次都历史都保留了reference l ...