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.

 The whole point to make 'into' helper is hide the inner reducer logic from user. So 'into' helper will check the target's type, based on the type, will use different inner reducer.
 
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的更多相关文章

  1. [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 ...

  2. Asp.Net Boilerplate Project 使用swagger调试api

    文件有点大,去掉了packages文件夹,(Swashbuckle.Core.5.6.0) 链接:https://pan.baidu.com/s/1DzMLhFyRav0dufS4dTeMzg 提取码 ...

  3. 第一篇:《Kubernetes 入门介绍》

    前言:本文是一篇 kubernetes(下文用 k8s 代替)的入门文章,将会涉及 k8s 的技术历史背景.架构.集群搭建.一个 Redis 的例子,以及如何使用 operator-sdk 开发 op ...

  4. 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 ...

  5. python--爬虫入门(八)体验HTMLParser解析网页,网页抓取解析整合练习

    python系列均基于python3.4环境  基本概念 html.parser的核心是HTMLParser类.工作的流程是:当你feed给它一个类似HTML格式的字符串时,它会调用goahead方法 ...

  6. windows通过thrift访问hdfs

    thirift是一个支持跨种语言的远程调用框架,通过thrift远程调用框架,结合hadoop1.x中的thriftfs,编写了一个针对hadoop2.x的thriftfs,供外部程序调用. 1.准备 ...

  7. python之HTMLParser解析HTML文档

    HTMLParser是Python自带的模块,使用简单,能够很容易的实现HTML文件的分析.本文主要简单讲一下HTMLParser的用法. 使用时需要定义一个从类HTMLParser继承的类,重定义函 ...

  8. docker中镜像的提交和上传

    本文介绍如何将本地的镜像上传到镜像仓库.以及上传时遇到"denied: requested access to the resource is denied"的解决方法. 原文地址 ...

  9. docker images

    docker images 介绍 镜像是动态的容器的静态表示,包括容器所要运行的应用代码以及运行时的配置.Docker镜像包括一个或者多个只读层(read-only layers),因此,镜像一旦被创 ...

随机推荐

  1. TCP的可靠传输(依赖流量控制、拥塞控制、连续ARQ)

    TCP可靠性表现在它向应用层提供的数据是无差错,有序,无丢失,即递交的和发送的数据是一样的. 可靠性依赖于流量控制.拥塞控制.连续ARQ等技术 <TCP/IP详解>中的“分组”是不是就是报 ...

  2. (转)Epoll模型详解

    1. 内核中提高I/O性能的新方法epoll epoll是什么?按照man手册的说法:是为处理大批量句柄而作了改进的poll.要使用epoll只需要这三个系统调 用:epoll_create(2),  ...

  3. 《iOS Human Interface Guidelines》——System Button

    系统button 系统button运行一个app特定的动作. API NOTE 在iOS 7中,UIButtonTypeRoundedRect被又一次定义成UIButtonTypeSystem.查看U ...

  4. VC6.0VB6.0 Scratch等软件

    VC6.0VB6.0 Scratch等软件 http://pan.baidu.com/s/1nv4hJrb

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

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

  6. ubuntu系统AndroidStudio改动内存大小

    位于android-studio/bin文件夹下的studio64.vmoptions和studio.vmoptions文件. 把Xms,Xmx,-XX:MaxPermSize.-XX:Reserve ...

  7. Yocto tips (19): Yocto SDK Toolchian的使用

    在使用之前须要先source env,导入各种环境变量(注意将路径变更成你自己的): source ../qt5_sdk/environment-setup-cortexa9hf-vfp-neon-p ...

  8. 安卓版微信自带浏览器和IE6浏览器ajax请求abort错误处理

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46419567 给页面元素绑定了一个click事件用来触发ajax请求.在安卓微信自 ...

  9. mpi之MPI_Sendrecv的用法

    mpi变成常用命令 编译c程序 gcc  例: gcc -Wall -o my_sa my_sa.c  若要编译c++,需要连接, 加参数 gcc -Wall -o my_sa my_sa.cpp - ...

  10. caffe环境配置

    参考:http://blog.csdn.net/enjoyyl/article/details/47397505 http://blog.csdn.net/baobei0112/article/det ...