[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 way to transduce into arrays and objects without having to specify the inner reducer behaviour, i.e. how to build up values, since a given collection type will almost always have the same inner reducer.
In this lesson we'll being doing just that with an into() helper function that will know if an array or object collection is passed in and handle each case accordingly.
import {isPlainObject, isNumber} from 'lodash';
import {compose, map, filter, pushReducer} from '../utils';
//current transduce
const transduce = (xf /** could be composed **/, reducer, seed, collection) => {
const transformedReducer = xf(reducer);
let accumulation = seed;
for (let value of collection) {
accumulation = transformedReducer(accumulation, value);
}
return accumulation;
};
const objectReducer = (obj, value) => Object.assign(obj, value);
const into = (to, xf, collection) => {
if (Array.isArray(to)) return transduce(xf, pushReducer, to, collection);
else if (isPlainObject(to)) return transduce(xf, objectReducer, to, collection);
throw new Error('into only supports arrays and objects as `to`');
};
into(
[],
compose(
map(x => x/2),
map(x => x * 10)
),
[1,2,3,4],
);
into(
{},
compose(filter(isNumber), map(val => ({[val]: val}))),
[1,2,3,4, 'hello', () => 'world'],
);
utils:
export const compose = (...functions) =>
functions.reduce((accumulation, fn) =>
(...args) => accumulation(fn(...args)), x => x); export const map = xf => reducer => {
return (accumulation, value) => {
return reducer(accumulation, xf(value));
};
}; export const filter = predicate => reducer => {
return (accumulation, value) => {
if (predicate(value)) return reducer(accumulation, value);
return accumulation;
};
};
export const pushReducer = (accumulation, value) => {
accumulation.push(value);
return accumulation;
};
[Transducer] Make an Into Helper to Remove Boilerplate and Simplify our Transduce API的更多相关文章
- [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 ...
- Asp.Net Boilerplate Project 使用swagger调试api
文件有点大,去掉了packages文件夹,(Swashbuckle.Core.5.6.0) 链接:https://pan.baidu.com/s/1DzMLhFyRav0dufS4dTeMzg 提取码 ...
- 第一篇:《Kubernetes 入门介绍》
前言:本文是一篇 kubernetes(下文用 k8s 代替)的入门文章,将会涉及 k8s 的技术历史背景.架构.集群搭建.一个 Redis 的例子,以及如何使用 operator-sdk 开发 op ...
- Displaying Data in a Chart with ASP.NET Web Pages (Razor)
This article explains how to use a chart to display data in an ASP.NET Web Pages (Razor) website by ...
- python--爬虫入门(八)体验HTMLParser解析网页,网页抓取解析整合练习
python系列均基于python3.4环境 基本概念 html.parser的核心是HTMLParser类.工作的流程是:当你feed给它一个类似HTML格式的字符串时,它会调用goahead方法 ...
- windows通过thrift访问hdfs
thirift是一个支持跨种语言的远程调用框架,通过thrift远程调用框架,结合hadoop1.x中的thriftfs,编写了一个针对hadoop2.x的thriftfs,供外部程序调用. 1.准备 ...
- python之HTMLParser解析HTML文档
HTMLParser是Python自带的模块,使用简单,能够很容易的实现HTML文件的分析.本文主要简单讲一下HTMLParser的用法. 使用时需要定义一个从类HTMLParser继承的类,重定义函 ...
- docker中镜像的提交和上传
本文介绍如何将本地的镜像上传到镜像仓库.以及上传时遇到"denied: requested access to the resource is denied"的解决方法. 原文地址 ...
- docker images
docker images 介绍 镜像是动态的容器的静态表示,包括容器所要运行的应用代码以及运行时的配置.Docker镜像包括一个或者多个只读层(read-only layers),因此,镜像一旦被创 ...
随机推荐
- IE6 浏览器常见兼容问题 共23个
1.<!DOCTYPE HTML>文档类型的声明. 产生条件:IE6浏览器,当我们没有书写这个文档声明的时候,会触发IE6浏览器的怪异解析现象: 解决办法:书写文档声明. 2.不同浏览器当 ...
- 监控Apache计数器
- 搭建一套Java开发环境以及使用eclipse从头一步步创建java项目
一.java 开发环境的搭建 在windows 环境下怎么配置环境. 1.首先安装JDK java的sdk简称JDK ,去其官方网站下载JDK. https://www.oracle.com/tech ...
- 洛谷——P2822 组合数问题
https://www.luogu.org/problem/show?pid=2822 题目描述 组合数C_n^mCnm表示的是从n个物品中选出m个物品的方案数.举个例子,从(1,2,3) 三 ...
- COGS——T 2342. [SCOI2007]kshort || BZOJ——T 1073
http://www.cogs.pro/cogs/problem/problem.php?pid=2342 ★★☆ 输入文件:bzoj_1073.in 输出文件:bzoj_1073.out ...
- JDK1.7中的ThreadPoolExecutor源代码剖析
JDK1. 7中的ThreadPoolExecutor 线程池,顾名思义一个线程的池子,池子里存放了非常多能够复用的线程,假设不用线程池相似的容器,每当我们须要创建新的线程时都须要去new Threa ...
- Node.js 博客实例(一)简单博客
原教程 https://github.com/nswbmw/N-blog/wiki/_pages的第一章.因为版本号等的原因,在原教程基础上稍加修改就可以实现. 环境: win7旗舰版64位 Node ...
- linux下开发,解决cocos2d-x中编译出现的一个小问题, undefined reference to symbol 'pthread_create@@GLIBC_2.2.5'
解决cocos2d-x中编译出现的一个小问题 对于cocos2d-x 2.×中编译中,若头文件里引入了#include "cocos-ext.h",在进行C++编译的时候会遇到例如 ...
- linux 命令 xxd
xxd,能够查看linux下文件的二进制表示.man一下xxd.能够得到下面信息 NAME xxd - make a hexdump or do the reverse. SYNOPSI ...
- 轻松学习JavaScript十七:JavaScript的BOM学习(二)
JavaScript计时事件 通过使用JavaScript中的BOM对象中的window对象的两个方法就是setTimeout()方法和claerTimeout()方法,我们 有能力作到在一个设定的时 ...