[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 ...
随机推荐
- URL中一些特殊符号的替代符
下表中列出了一些URL特殊符号及编码 十六进制值 1.+ URL 中+号表示空格 %2B 2.空格 URL中的空格可以用+号或者编码 %20 3./ 分隔目录和子目录 %2F 4.? 分隔实际的 UR ...
- cogs 466. [NOIP2009] 细胞分裂
466. [NOIP2009] 细胞分裂 ★★ 输入文件:cell.in 输出文件:cell.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] Hanks ...
- Qt程序打包成exe可执行文件
很多Qt爱好者想发布自己的Qt软件,但却发现在其他没有安装Qt SDK的机器上无法运行,这就是本文想要说明的问题.现在网上大部分软件都要发布自己开发的应用程序,都会打包到exe文件中,待安装完exe文 ...
- 【JavaEE WEB 开发】Tomcat 具体解释 Servlet 入门
转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/47146817 一. Tomcat 下载安装配置 1. Tomcat 下载 T ...
- hdu 4882 ZCC Loves Codefires(贪心)
# include<stdio.h> # include <algorithm> # include <string.h> using namespace std; ...
- UBUNTU 16.04 下安装动态链接库方法(使用ln命令可以随意映射动态库,ldd查看缺少的动态库)
一般先使用ldd 来查看该应用程序缺少什么东西,然后,再根据sudo apt install XXX 去安装相应的动态库. 假如没有对应的库,可以使用: sudo ln -s /usr/lib/lib ...
- 26.boost文件库
#define _CRT_SECURE_NO_WARNINGS #include <boost/filesystem/operations.hpp> #include <boost/ ...
- POJ 1951 模拟
思路: 坑爹模拟毁我一生 给两组数据: 输入: YOURE TRAVELING THROUGH ANOTHER DIMENSION A DIMENSION NOT OF SIGHT. 输出: YR T ...
- html 移动端关于长按图片弹出保存问题
在做html5项目的时候有个需求是要拖动一个图片,但是又不要用户长时间按着弹出保存框.首先想到的就是在点图片的时候阻止默认事件的发生: js停止冒泡· function myfn(e){ window ...
- 读书笔记-构建高性能Web站点
基本概念 带宽:通常说的带宽比如8M带宽,是指主机与互联网运营商的交换机之间的数据传输速度,因为数据链路层的流量是通过控制接收方实现的.而百兆网卡则是指网卡的发送速度为100Mbit/s,则是指网卡发 ...