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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Functional programming

    In computer science, functional programming is a programming paradigm, a style of building the struc ...

  4. Java 中的函数式编程(Functional Programming):Lambda 初识

    Java 8 发布带来的一个主要特性就是对函数式编程的支持. 而 Lambda 表达式就是一个新的并且很重要的一个概念. 它提供了一个简单并且很简洁的编码方式. 首先从几个简单的 Lambda 表达式 ...

  5. Monad (functional programming)

    In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...

  6. Beginning Scala study note(4) Functional Programming in Scala

    1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...

  7. Functional Programming without Lambda - Part 2 Lifting, Functor, Monad

    Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...

  8. Functional programming idiom

    A functional programming function is like a mathematical function, which produces an output that typ ...

  9. 关于函数式编程(Functional Programming)

    初学函数式编程,相信很多程序员兄弟们对于这个名字熟悉又陌生.函数,对于程序员来说并不陌生,编程对于程序员来说也并不陌生,但是函数式编程语言(Functional Programming languag ...

随机推荐

  1. 洛谷P2151 [SDOI2009] HH去散步 [矩阵加速]

    题目传送门 HH去散步 题目描述 HH有个一成不变的习惯,喜欢饭后百步走.所谓百步走,就是散步,就是在一定的时间 内,走过一定的距离. 但是同时HH又是个喜欢变化的人,所以他不会立刻沿着刚刚走来的路走 ...

  2. NEO4j简单入门

    Neo4j是: 一个开源 无Schema 没有SQL 图形数据库 图形数据库也称为图形数据库管理系统或GDBMS. Neo4j的官方网站:http://www.neo4j.org Neo4j的优点 它 ...

  3. shell 空间利用

    root #df -h / Filesystem Size Used Avail Use% Mounted on /dev/sda3 3.3G 1.7G 1.5G 54% /

  4. aop相关术语

    AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面的编程.并不是全部的AOP框架都是一样的.他们连接点模型的功能可能有强弱之分,有些可以字段,方法,构 ...

  5. XPath中的text()和string()区别(转)

    原文地址 : http://blog.csdn.net/jiangchao858/article/details/63314426 本质区别 text()是一个node test,而string()是 ...

  6. java 安全 技术

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 加密对应的类 是 Cipher ,意思 是加密的意思.这个类 在  javax.cryp ...

  7. android studio 汉化 美化 个性化 修改 安卓工作室 2.3.3 最新版

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 先看一下效果. 建议全屏看图,或者新标签看图.

  8. Entity Framework(实体框架 EF)

    什么是Entity Framework呢(下面简称EF)? EF(实体框架)是ADO.NET中的一组支持开发面向数据的软件应用程序的技术,是微软的一个ORM框架.ORM(对象关系映射框架):指的是面向 ...

  9. Linux下C语言多文件的编译以及makefile的应用

    1.关于编译和链接 一般来说,无论是C.C++,首先要把源文件编译成中间代码文件,在Windows下也就是.obj文件,UNIX下是.o文件,即Object File,这个动作叫做编译(compile ...

  10. Codeforces 992 E. Nastya and King-Shamans

    \(>Codeforces\space992 E. Nastya and King-Shamans<\) 题目大意 : 给你一个长度为 \(n\) 的序列,有 \(q\) 次操作,每一次操 ...