[Functional Programming] Unbox types with foldMap
Previously we have seen how to use Concat with reduce:
const res5 = [Sum(), Sum(), Sum()]
.reduce((acc, x) => acc.concat(x), Sum.empty());
console.log("res5", res5); // Sum(6)
To simply this, we can use 'fold':
const {Map, List} = require('immutable-ext'); const res6 = List.of(Sum(), Sum(), Sum())
.fold(Sum.empty());
console.log("res6", res6);
Javascript arrray doesn't have 'fold' so we use immutable-ext's List.
We can also use Map:
const res7 = Map({brian: Sum(), sara: Sum()})
.fold(Sum.empty());
console.log("res7", res7); // Sum(11)
Normally, we don't have object contains Functor as values, then the easy way is mapping to Sum type:
const res8 = Map({brian: , sara: })
.map(Sum)
.fold(Sum.empty());
console.log("res8", res8);
First Mapping then fold is common use case, then we can use a shortcut opreator called 'foldMap':
const res9 = Map({brian: , sara: })
.foldMap(Sum, Sum.empty());
console.log("res9", res9);
[Functional Programming] Unbox types with foldMap的更多相关文章
- Functional Programming without Lambda - Part 1 Functional Composition
Functions in Java Prior to the introduction of Lambda Expressions feature in version 8, Java had lon ...
- a primary example for Functional programming in javascript
background In pursuit of a real-world application, let’s say we need an e-commerce web applicationfo ...
- Functional programming
In computer science, functional programming is a programming paradigm, a style of building the struc ...
- Java 中的函数式编程(Functional Programming):Lambda 初识
Java 8 发布带来的一个主要特性就是对函数式编程的支持. 而 Lambda 表达式就是一个新的并且很重要的一个概念. 它提供了一个简单并且很简洁的编码方式. 首先从几个简单的 Lambda 表达式 ...
- Monad (functional programming)
In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...
- Beginning Scala study note(4) Functional Programming in Scala
1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...
- Functional Programming without Lambda - Part 2 Lifting, Functor, Monad
Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...
- Functional programming idiom
A functional programming function is like a mathematical function, which produces an output that typ ...
- 关于函数式编程(Functional Programming)
初学函数式编程,相信很多程序员兄弟们对于这个名字熟悉又陌生.函数,对于程序员来说并不陌生,编程对于程序员来说也并不陌生,但是函数式编程语言(Functional Programming languag ...
随机推荐
- SCU 4442 Party
二分图的最小点权覆盖. 非常感谢巨巨@islands_的解答,还帮我画了一个图. 题目保证给出的边构成的图是一个二分图. 如果没有第三种类型的$frog$,那么问题就很简单了.即选择哪些点,覆盖住所有 ...
- GDB 调试工具高级用法
解决core核心文件转出问题 ulimit -c #查看core文件的生成开关,若为0则关闭 ulimit -c unlimited #打开开关,只在当前shell生效 sudo sh -c 'ech ...
- Unity 2D游戏开发教程之使用脚本实现游戏逻辑
Unity 2D游戏开发教程之使用脚本实现游戏逻辑 使用脚本实现游戏逻辑 通过上一节的操作,我们不仅创建了精灵的动画,还设置了动画的过渡条件,最终使得精灵得以按照我们的意愿,进入我们所指定的动画状态. ...
- Linux通过FTP上传文件到服务器
1.如果没有安装ftp,可执行: 输入:yum -y install ftp,回车 等待安装完毕 2.连接服务器 输入:ftp 服务器IP,回车 根据提示输入用户名和密码 3.上传下载操作 1). 上 ...
- 【UOJ #204】【APIO 2016】Boat
http://uoj.ac/problem/204 肯定要离散化的,先离散化出\(O(n)\)个取值区间. 设\(f(i,j)\)表示第\(i\)所学校派出的划艇数量在\(j\)区间中. \(f(i, ...
- [APIO2018]铁人两项 --- 圆方树
[APIO2018] 铁人两项 题目大意: 给定一张图,问有多少三元组(a,b,c)(a,b,c 互不相等)满足存在一条点不重复的以a为起点,经过b,终点为c的路径 如果你不会圆方树 ------- ...
- hdu 1711
读入优化有3s多. #include <cstdio> #include <cctype> #define maxn 1000010 #define maxm 10010 in ...
- PYPY_GC
Author:Jin Date: 2014-7-8 http://doc.pypy.org/en/latest/windows.html http://www.pypy.org/download.ht ...
- dsh安装指南
dsh批量管理linux服务器 一,安装dsh 切换到root用户,然后切换到work_home_yourname wget http://www.netfort.gr.jp/~dancer/soft ...
- 更新yum源/apt-get源
国内开源镜像站有:网易: http://mirrors.163.com/ 搜狐: http://mirrors.sohu.com/阿里云: http://mirrors.aliyun.com/北京理工 ...