[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 ...
随机推荐
- python在不同情况下写入csv文件
情况一(解法一):将列表存储为csv文件.列表的每一项代表csv文件的一行. 列表中的每一项包含多个属性.list=[[属性1,属性2,属性3,……],[属性1,属性2,属性3,……],[属性1,属性 ...
- js实现点击复制网页内容(基于execCommand)
通过execCommand方法来实现,当一个HTML文档切换到设计模式 designMode时,文档对象暴露 execCommand 方法,该方法允许运行命令来操纵可编辑区域的内容.大多数命令影响文档 ...
- Tomcat远程代码执行漏洞(CVE-2017-12615)修复
一.漏洞介绍 2017年9月19日,Apache Tomcat官方确认并修复了两个高危漏洞,其中就有Tomcat远程代码执行漏洞,当存在漏洞的Tomcat运行在Windwos主机上,且启用了HTTP ...
- Android自己定义百度地图缩放图标
自己定义实现Android百度地图的缩放图标,须要自己定义一个缩放控件,实现效果例如以下: 这里的缩放效果,实现了点击button能够对地图的放大缩小,通过手势放大与缩小也控制缩放图标的可用状态.详细 ...
- Nodejs之旅開始
web前端是一个门槛低,但精通起来比較难的行业,由于它涉及的范围比較广,也许在十年前.我光靠切图,就能找到一个好的职位,可是如今,仅仅会切图.我们非常难找到自己惬意的工作,如今前端职位要求不仅是htm ...
- Android技术归档
各位小伙伴们.以后小巫的一些开源码都会上传到github中,所以欢迎大家Follow https://github.com/devilWwj 基于眼下我基本的技术领域在Android上,以后关于And ...
- mydumper安装及安装故障汇总
mydumper是针对mysql数据库备份的一个轻量级第三方的开源工具,备份方式术语逻辑备份.它支持多线程.备份速度远高于原生态的mysqldump以及众多优异特性. 因此该工具是DBA们的不二选 ...
- php利用msqli访问数据库并实现分页,
<?php require_once 'login.php'; $num_rec_per_page=2; // 每页显示数量 //mysql_connect('localhost','jim', ...
- apiCloud中openFrameGroup传参
apiCloud中openFrameGroup传参 1.无效的 api.openFrameGroup({ // 打开 frame 组 name: 'group', scrollEnabled: fal ...
- Android 手势
GestureDetector:手势监听类,通常在View的setOnTouchListener方法中设置TouchListener,在TouchListener的onTouch函数中把MotionE ...