[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 ...
随机推荐
- keil中的memory model
这两天仿真遇到的怪事真的是一大堆. 还是读写Flash的代码.keil编译OK,但是仿真就是莫名其妙地挂掉出现一些乱七八糟的事情. 后面发现是keil 中的memory model勾选错了,勾选的是l ...
- javascript 中关于function中的prototype
在javascrpit中每个函数中都有一个prototype属性,在其创建的时候,无论是用var method = function(){}或者 var method = new Function() ...
- markdown入门语法
目录 一: 标题 二:字体 三: 引用 四:分割线 五:图片 六:超链接 七:列表 九: 代码块 一: 标题 # 一级标题 ## 二级标题 ####### 最大六级标题 二:字体 1. 加粗字体 加粗 ...
- 分割视图控制器(UISplitViewController)
这种控制器只能用于iPad,它可以在iPad屏幕中显示两个不同的场景:在横向模式下,左边显示一个表,供用户选择:用户选择表中的元素后,详细视图将显示该元素的详细信息.如果iPad被旋转到纵向模式,表将 ...
- 详解Ubuntu Server下启动/停止/重启MySQL数据库的三种方式(ubuntu 16.04)
启动mysql: 方式一:sudo /etc/init.d/mysql start 方式二:sudo service mysql start 停止mysql: 方式一:sudo /etc/init.d ...
- 入门cout输出的格式(补位和小数精度)
http://blog.csdn.net/gentle_guan/article/details/52071415 mark一下,妈妈再也不用担心我高精度不会补位了
- 【二项式定理】【DFS】UVALive - 7639 - Extreme XOR Sum
题意:一个序列,q次询问,每次问你某个指定区间内的EXtreme XOR值. 一个长度为l的区间的EXtreme XOR值被定义为,从左到右,将每相邻的两个数XOR起来,产生l-1个新的值,……如此循 ...
- bzoj 4097: [Usaco2013 dec]Vacation Planning
4097: [Usaco2013 dec]Vacation Planning Description Air Bovinia is planning to connect the N farms (1 ...
- 新浪微博API的使用Python
本文记录了用新浪微博官方Python SDK调用API进行开发的流程. 准备工作 申请成为开发者并创建一个应用: 首先要有一个新浪微博的账号,然后去新浪微博开放平台(http://open.weibo ...
- bzoj1036 count 树链剖分或LCT
这道题很久以前用树链剖分写的,最近在学LCT ,就用LCT再写了一遍,也有一些收获. 因为这道题点权可以是负数,所以在update时就要注意一下,因为平时我的0节点表示空,它的点权为0,这样可以处理点 ...