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:条件渲染和列表渲染

    条件渲染 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...

  2. JS深拷贝拷贝的区别?

    拷贝拷贝引用,共享内存 深拷贝拷贝实例,不共享内存   1. 浅拷贝:当一个对象拷贝另一个对象的数据时,只要一个对象的数据发生改变时,另一个对象的数据也会发生改变,因为浅拷贝拷贝的是引用的地址 实现方 ...

  3. 05006_Linux的jdk、mysql、tomcat安装

    1.软件包下载链接:软件包下载 密码:advk 2.安装JDK (1)查看当前Linux系统是否已经安装java,输入 rpm -qa | grep java : (2)卸载两个openJDK (3) ...

  4. if判断语句

     6)if判断语句   if ... then   else   end if;     if ... then   elsif ... then   elsif ... then   else   ...

  5. oracle间隔分区

    http://blog.csdn.net/rznice/article/details/55048876

  6. 数据结构与算法系列----最小生成树(Prim算法&amp;Kruskal算法)

     一:Prim算法       1.概览 普里姆算法(Prim算法).图论中的一种算法.可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中.不但包含了连通图里的全部顶点(英语:Ve ...

  7. Android开发工具---SQLiteManager插件

    Android开发工具---SQLiteManager插件 效果图例如以下: 平时在开发过程中查看数据库都要把数据库文件导出来,然后再用其它工具打开,SQLiteManager插件则给予我们一些便利. ...

  8. CentOS6.3升级GCC到GCC4.8.2

    server上安装的GCC版本号过旧.已经不满足个人使用的版本号需求,故决定对其进行升级操作.由当前版本号3.4.6升级到4.8.2.然受权限制约.仅仅能安装到个人文件夹.因此假设您的server能够 ...

  9. sql 查询所有数据库-表-表结构

    --查询数据库中的所有数据库名: SELECT * FROM Master..SysDatabases ORDER BY Name --查询某个数据库中所有的表名: select * from sys ...

  10. 51nod 1065 最小正字段和 解决办法:set存前缀和,二分插入和二分查找

    题目: 这题要求大于0的最小字段和,常规O(n)求最大字段和的方法肯定是没法解的. 我的解法是:用sum[i]存前i项的和,也就是前缀和. 这题就变成了求sum[j]-sum[i]的大于0的最小值( ...