So far we've been transducing by manually calling .reduce() on arrays, but we want to be able to transduce over other collection types as well.

In this lesson we'll create a transduce function to support transducing over any data structure that implements the es2015 iterable protocol. We’ll put it to the test by transducing over strings and maps, as well as going from one collection type as input to another as output.

The whole point to make transducer work for all iteratable collection is that, iteratable collction can be Map, TypedArray, Array, Set and String.

Current the implement from last post:

[1, 2, 3, 4].reduce(
compose(isNot2Filter, isEvenFilter, doubleMap)(pushReducer),
[],
); const transduce = (xf, reducer, seed, collection) => {
collection.reduce(xf(reducer), seed)
}

This implementaion only works for Array type.

If we want Transducer can be used for all Itertable type, we need to change by using 'reduce' to 'for...of' loop.

const transduce = (xf, reducer, seed, collection) => {
const transformedReducer = xf(reducer);
let accumulation = seed;
for (const value of collection) {
accumulation = transformedReducer(accumulation, value);
} return accumulation;
}

Now that, we can use for any iteratable collection not only Array type:

const toUpper = str => str.toUpperCase();
const isVowel = char => ['a', 'e', 'i', 'o', 'u', 'y'].includes(char.toLowerCase()); transduce(
compose(map(toUpper), filter(isVowel)),
(str, char) => str + char,
'',
'adrian',
); const numMap = new Map();
numMap.set('a', 1);
numMap.set('b', 2);
numMap.set('c', 3);
numMap.set('d', 4); transduce(
compose(isNot2Filter, isEvenFilter, doubleMap),
pushReducer,
[],
numMap.values(),
);

[Javascript] Transduce over any Iteratable Collection的更多相关文章

  1. [Transducer] Make Transducer works for Iteratable collection and Object

    We've seen how we can transduce from arrays or other iterables, but plain objects aren't iterable in ...

  2. [Transducer] Create a Sequence Helper to Transduce Without Changing Collection Types

    A frequent use case when transducing is to apply a transformation to items without changing the type ...

  3. [label][JavaScript]闭包阅读笔记

    原文链接来源:                       http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures.ht ...

  4. [Transducer] Make an Into Helper to Remove Boilerplate and Simplify our Transduce API

    Our transduce function is powerful but requires a lot of boilerplate. It would be nice if we had a w ...

  5. SequoiaDB 系列之三 :SequoiaDB的高级功能

    上一篇简单描述了一下SequoiaDB的简单CRUD操作,本篇将讲述一下稍微高级点的功能. 部署在我机器上的集群环境,在经过创建名字为"foo"的cs,创建名字为"bar ...

  6. Qt WebEngine 网页交互

    环境:Qt5.7.0,VS2013 一.简单介绍 从 Qt5.4 开始已经去掉 Qt WebKit 模块了,使用的是 chrome 内核封装的 QtWebEngine,浏览器相关的类有以下几个: QW ...

  7. [Backbone]Make Backbone Better With Extensions

    Backbone is becoming wildly popular as a web application development framework. Along with this popu ...

  8. Bootstrap优秀模板-INSPINIA.2.9.2

    下载量最高的Bootstrap管理端模板,完美适配H5,.NET COre.MVC5.Ruby on Rails多种开发环境. 下面是官方介绍:INSPINIA Admin Theme is a pr ...

  9. Object 和 JSON 区别联系

    JavaScript Object-based JavaScript is almost entirely object-based. Object name Object property name ...

随机推荐

  1. vue定义对象变量并合并成新的对象

    背景: 一般情况下,向后台发送数据请求会存在公共的变量,为了避免每一个相同部分的变量都重新定义,则想出以下解决方案: 例如一下:function,version,Authorization是公共请求部 ...

  2. 安卓版微信自带浏览器和IE6浏览器ajax请求abort错误处理

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46419567 给页面元素绑定了一个click事件用来触发ajax请求.在安卓微信自 ...

  3. java ee5的新特性

    1.标注 一种元数据,作用分为三类:编写文档@Document.代码分析@Deparecated(过时的)和编译检查@override(重写) 2.EJB3 EJB2的升级版,商业化的java bea ...

  4. [linux]shell中,反引號(`)的应用

    反引號位 (`) 位于键盘的Tab键的上方.1键的左方.注意与单引號(')位于Enter键的左方的差别. 反引號位 (`)在Linux中起着命令替换的作用. 命令替换是指shell可以将一个命令的标准 ...

  5. node10---GET请求和POST请求的参数

    GET请求的参数在URL中,在原生Node中,需要使用url模块来识别参数字符串.在Express中,不需要使用url模块了.可以直接使用req.query对象. ● POST请求在express中不 ...

  6. ORA-00980:synonym translation is no longer valid

    今天要把测试环境DB的数据更新成最新Production环境的数据,期间发生了一些问题: 1.首先从正式环境exp出想要用户的dmp档 2.drop掉测试环境底下相应用户 3.create测试环境底下 ...

  7. 记录一下c++的一点指针所得

    c++有两种传值可以改变外部参数,一种是传递指针,另一种是传递引用,对于前者,可以用Ugly(but explicitly),对于后者,Clean(but hidden),在传递的时候有一种值得注意的 ...

  8. sublime配置python运行环境

    1.sublime下载与插件管理 1.1 下载 官网地址:https://www.sublimetext.com/3 1.2 安装Package Control管理插件 使用ctrl + ` (感叹后 ...

  9. Ubuntu16.04下Mongodb官网卸载部署步骤(图文详解)(博主推荐)

    不多说,直接上干货! 前期博客 Ubuntu16.04下Mongodb官网安装部署步骤(图文详解)(博主推荐) https://docs.mongodb.com/manual/tutorial/ins ...

  10. Linux基础03

    ** Linux基本操作常用命令(三) ** Linux的软件包 Linux的软件把分为“源码包”和“二进制包” 源码包:免费,开源 二进制包:系统默认包,即RPM包(上一节我们通过rpm卸载过ope ...