[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([,,]));
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的更多相关文章
- [Compose] 15. Applicative Functors for multiple arguments
Working our way backwards from solution to problem, we define an applicative functor, then use it to ...
- [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 ...
- [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, ...
- <STL源码剖析> 6.3.6 power
计算power的算法说明 http://www.sxt.cn/u/324/blog/2112 翻译自 http://videlalvaro.github.io/2014/03/the-power-a ...
- Haskell学习-functor
原文地址:Haskell学习-functor 什么是Functor functor 就是可以执行map操作的对象,functor就像是附加了语义的表达式,可以用盒子进行比喻.functor 的定义可以 ...
- Function Composition vs Object Composition
In functional programming, we create large functions by composing small functions; in object-oriente ...
- [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 ...
- 浅释Functor、Applicative与Monad
引言 转入Scala一段时间以来,理解Functor.Applicative和Monad等概念,一直是我感到头疼的部分.虽然读过<Functors, Applicatives, And Mona ...
- Docker之Compose服务编排
Compose是Docker的服务编排工具,主要用来构建基于Docker的复杂应用,Compose 通过一个配置文件来管理多个Docker容器,非常适合组合使用多个容器进行开发的场景. 说明:Comp ...
随机推荐
- COMP COMP-3
Comp (Computational) Comp (with no suffix) leaves the choice of the data type to the compiler writer ...
- Python操作Mongo数据库
连接数据库 import pymongo # 连接到数据库,以下两种方式均可 client = pymongo.MongoClient(host='localhost', port=27017) cl ...
- 【UOJ 179】 #179. 线性规划 (单纯形法)
http://uoj.ac/problem/179 补充那一列修改方法: 对于第i行: $$xi=bi-\sum Aij*xj$$ $$=bi-\sum_{j!=e} Aij*xj-Aie*xe ...
- CSS 笔记——阴影、圆角、旋转、光标
7. 阴影.圆角.旋转.光标 (1)box-shadow 阴影 基本语法 text-shadow: h-shadow v-shadow blur color; box-shadow: h-shadow ...
- [CF626F]Group Projects
[CF626F]Group Projects 题目大意: 有一个长度为\(n(n\le200)\)的数列\(\{A_i\}\),将其划分成若干个子集,每个子集贡献为子集\(\max-\min\).求子 ...
- 某DP题目1
题意: 有n个由左右括号组成的字符串,选择其中若干字符串,使得组成的括号序列合法且长度最长.n <= 1000,n个字符串的长度和 <= 10000. 分析: 其实我一开始做这一题的时候, ...
- php-streams扩展学习
一. streams是干嘛的: 用于统一文件.网络.数据压缩等类文件操作方式,并为这些类文件操作提供一组通用的函数接口. 二. stream是具有流式行为的资源对象,这个对象有一个包装类 例如: pr ...
- [转]Android Service完全解析,关于服务你所需知道的一切
目录(?)[+] Android Service完全解析,关于服务你所需知道的一切(上) 分类: Android疑难解析2013-10-31 08:10 6451人阅读 评论(39) 收藏 举报 ...
- HDU 4612 Warm up tarjan 树的直径
Warm up 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4612 Description N planets are connected by ...
- 用python slearning类库实现数据挖掘(python3.x)
Summary of test0 data : source data source code : in test0 file reference : - Reference Website / - ...