[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),因此,镜像一旦被创 ...
随机推荐
- 查看系统进程:ps、top
1.ps命令:提供最近进程的快照.显示当前活跃进程的简要信息. 常见使用: (1)与grep命令配合查找是否有相应进程存活 ps -ef | grep ksmd ps -Af | grep ksmd ...
- vue自定义组件并使用
以下是使用自己写的一个简单的文件上传框为例 1.自定义组件结构(一个js文件,一个vue文件),最好单独放一个文件 2.upload.vue 内容 其中,action是父组件传递给子组件的参数,使用p ...
- Struts2SpringHibernate整合示例,一个HelloWorld版的在线书店(项目源码+详尽注释+单元测试)
Struts2,Spring,Hibernate是Java Web开发中最为常见的3种框架,掌握这3种框架是每个Java Web开发人员的基本功. 然而,很多初学者在集成这3个框架的时候,总是会遇到各 ...
- vue中的生命周期
vue中的生命周期 1,vue生命周期简介: 1.beforeCreate 在实例初始化之后,数据观测和event/watcher时间配置之前被调用. 2.created 实例已经创建完成之后被调 ...
- word上怎么打钩
原文:http://www.wordlm.com/html/2291.html 有时我们在Word中制作一份特殊的表格时,可能会用到这样一些特殊符号,“在方框里打钩”或打叉.这些符号虽看起来简单不起眼 ...
- 【hihocoder 1519】 逃离迷宫II
[题目链接]:http://hihocoder.com/problemset/problem/1519?sid=1098756 [题意] Chinese [题解] bfs题; 根据bfs的性质; 第一 ...
- 【codeforces 746E】Numbers Exchange
[题目链接]:http://codeforces.com/problemset/problem/746/E [题意] 你有n张卡片,上面写着不同的数字; 然后另外一个人有m张上面写着不同的数字的卡片: ...
- YAML说明
YAML说明 https://www.cnblogs.com/songchaoke/p/3376323.html XML的简化
- python监控linux性能以及进程消耗的性能
# -*- coding: utf-8 -*- """ Created on Tue Jun 10 10:20:13 2014 @author: lifeix " ...
- Ansible@一个高效的配置管理工具--Ansible configure management--翻译(七)
如无书面授权,请勿转载 Larger Projects Until now, we have been looking at single plays in one playbook file. Th ...