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. php 文件加载方式

    两种加载文件的方式 include require 使用场景: 动态加载文件的时候,使用include,否则使用require. 示例: # 引入php文件--include方式 inlcude(&q ...

  2. 全面的framebuffer详解

    一.FrameBuffer的原理    FrameBuffer 是出现在 2.2.xx 内核当中的一种驱动程序接口.    Linux是工作在保护模式下,所以用户态进程是无法象DOS那样使用显卡BIO ...

  3. bat启动.exe的应用程序

    新建一个文本文档,编写如下,完成后保存将后缀名txt改为bat即可. rem 启动***(要启动的服务名) @echo off rem  程序安装的顶层目录 d: rem 设置显示文字颜色 color ...

  4. Manacher(最大回文字串)

    很好的讲解 注意两端的字符要不同,同时数组要开大一些 [Manacher]最长回文子串 #include<bits/stdc++.h> #define REP(i, a, b) for(r ...

  5. 使用展开操符作替代 .apply() (prefer-spread)

    在ES2015以前,你必须使用Function.prototype.apply()来调用可变函数. var args = [1, 2, 3, 4]; Math.max.apply(Math, args ...

  6. 【hihocoder 1519】 逃离迷宫II

    [题目链接]:http://hihocoder.com/problemset/problem/1519?sid=1098756 [题意] Chinese [题解] bfs题; 根据bfs的性质; 第一 ...

  7. java深克隆与浅克隆

    2015.9.19 6:45   星期五    1

  8. HTTP请求和响应模式(B/S)(2)

    B/S          及浏览器/客服端模式 根据发送的状态码不同,显示response的状态不同

  9. WinServer-PowerShell基础

    命令的参数: [-name] 这个参数必须要有,string表示name参数接受什么样的实参,<>表示参数可以接受的实参类型,通常出现set get add都会伴随着必须参数 [-name ...

  10. c++友元实现操作符重载

    运算符重载的本质是一个函数 #include <iostream> using namespace std; class A { private: int m_a; int m_b; fr ...