[Javascript] Transduce over any Iteratable Collection
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的更多相关文章
- [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 ...
- [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 ...
- [label][JavaScript]闭包阅读笔记
原文链接来源: http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures.ht ...
- [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 ...
- SequoiaDB 系列之三 :SequoiaDB的高级功能
上一篇简单描述了一下SequoiaDB的简单CRUD操作,本篇将讲述一下稍微高级点的功能. 部署在我机器上的集群环境,在经过创建名字为"foo"的cs,创建名字为"bar ...
- Qt WebEngine 网页交互
环境:Qt5.7.0,VS2013 一.简单介绍 从 Qt5.4 开始已经去掉 Qt WebKit 模块了,使用的是 chrome 内核封装的 QtWebEngine,浏览器相关的类有以下几个: QW ...
- [Backbone]Make Backbone Better With Extensions
Backbone is becoming wildly popular as a web application development framework. Along with this popu ...
- Bootstrap优秀模板-INSPINIA.2.9.2
下载量最高的Bootstrap管理端模板,完美适配H5,.NET COre.MVC5.Ruby on Rails多种开发环境. 下面是官方介绍:INSPINIA Admin Theme is a pr ...
- Object 和 JSON 区别联系
JavaScript Object-based JavaScript is almost entirely object-based. Object name Object property name ...
随机推荐
- ansible 工作原理以及使用详解
内容:1.ansible的作用以及工作结构2.ansible的安装以及使用3.ansible的playbook使用 一.ansible的作用以及工作结构 1.ansible简介: ...
- 通过CSS样式隐藏百度版权标志
在JSP中添加: //隐藏所有.anchorBL{ display:none; } //隐藏下方的保留百度地图图片 .BMap_cpyCtrl{ display:none; } 注:维护他人版权, ...
- (转)C++引用
前言:引用是C++一个很重要的特性,最近看了很多有关引用的资料和博客,故在此对引用的相关知识进行总结 一.什么是引用 引用,顾名思义是某一个变量或对象的别名,对引用的操作与对其所绑定的变量或对象的操作 ...
- JavaString库
String库 .length() 字符串的长度,一个字符串为空(空字符串对象)和null(不指向任何对象)是两个概念,中文字符和英文字符是一样的计数(一个中文是一个字符,一个英文字母是一个字符) . ...
- HDU 4418 高斯消元法求概率DP
把两种状态化成2*n-2的一条线上的一种状态即可.很容易想到. 高斯列主元法,不知为什么WA.要上课了,不玩了...逃了一次课呢.. #include <iostream> #includ ...
- 外面的wifi非常精彩,外面的wifi非常不安
星期一果然非常忙,看到安卓漏洞还是午休的时候.可能我们都习惯了.我们的信息安全一向难以得到保障.对于我来说,可能都无所谓了.可是作为用户之中的一个,我们也不能太安分,该须要的保障还是得维护. 本来.我 ...
- Math类概述及其成员方法
Math 类包括用于运行基本数学运算的方法,如初等指数.对数.平方根和三角函数,这个类寻常开发中用的不多,可是在某些需求上会用到,比方求二个用户年龄的相差多少岁,这会用到Math类中的方法!如今把Ma ...
- (转载)自定义View——弹性滑动
滑动是Android开发中非常重要的UI效果,几乎所有应用都包含了滑动效果,而本文将对滑动的使用以及原理进行介绍. 一.scrollTo与ScrollBy View提供了专门的方法用于实现滑动效果,分 ...
- Java事件处理机制1
实现一个小程序,怎样通过点击不同的按钮,让面板的背景色发生相应的变化,如图: public class Demo2 extends JFrame implements ActionListener{ ...
- centos下nginx配置
转自 http://www.linuxidc.com/Linux/2016-09/134907.htm 安装所需环境 Nginx 是 C语言 开发,建议在 Linux 上运行,当然,也可以安装 Wi ...