[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 ...
随机推荐
- nyoj 214 单调递增子序列(二)
单调递增子序列(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 ,a2...,an}(0<n<=100000),找出单调递增最长子序列,并求出其长度. ...
- java 安全 技术
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 加密对应的类 是 Cipher ,意思 是加密的意思.这个类 在 javax.cryp ...
- appcompat_v7怎么不创建
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 我们建项目时直接把最小SDK选在Android4.0以上不就不需要这个支持库
- BZOJ3881 Coci2015 Divljak fail树+差分
题目大意,给出两个字符串集合S和T,向T中添加字符串,查询S_i在T中有几个字符串出现过.一看这种多字符串匹配问题,我们联想到了AC自动机,做法就是,对于S集合我们建立一个AC自动机,建出fail树, ...
- 「学习笔记」wqs二分/dp凸优化
[学习笔记]wqs二分/DP凸优化 从一个经典问题谈起: 有一个长度为 \(n\) 的序列 \(a\),要求找出恰好 \(k\) 个不相交的连续子序列,使得这 \(k\) 个序列的和最大 \(1 \l ...
- POJ2222 Keywords Search AC自动机模板
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
- git -- git clone
git clone 命令参数: usage: git clone [options] [--] <repo> [<dir>] -v, --verbose be more ver ...
- ACM -- 算法小结(八)字符串算法之Manacher算法
字符串算法 -- Manacher算法 首先介绍基础入门知识,以下这部分来着一贴吧,由于是很久之前看的,最近才整理一下,发现没有保存链接,请原创楼主见谅. //首先:大家都知道什么叫回文串吧,这个算法 ...
- Codeforces Beta Round #8 A. Train and Peter KMP
A. Train and Peter 题目连接: http://www.codeforces.com/contest/8/problem/A Description Peter likes to tr ...
- HDU 4611 Balls Rearrangement 数学
Balls Rearrangement 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4611 Description Bob has N balls ...