[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 of the collection.
In this lesson, we'll create a helper to do just that. seq will be similar to into, except the target type will be inferred from the source collection. For example, if transducing from an array, seq will create an array as the output collection as well.
seq is thus more restrictive and easier to consume than into.
The whole for 'seq' is we don't need to worry about collection type will different from target type. In short, input and output are the same type.
import {isPlainObject} from 'lodash';
import {compose, map, arrayReducer, objectReducer, transduce} from '../utils.js';
const into = (to, xf, collection) => {
if (Array.isArray(to)) return transduce(xf, arrayReducer, to, collection);
else if (isPlainObject(to)) return transduce(xf, objectReducer, to, collection);
throw new Error('into only supports arrays and objects as `to`');
};
const seq = (xf, collection) => {
if (Array.isArray(collection)) return transduce(xf, arrayReducer, [], collection);
else if (isPlainObject(collection)) return transduce(xf, objectReducer, {}, collection);
throw new Error('unsupported collection type');
};
seq(map(x => x*2), [1,2,3]);
const flip = compose(
map(([k,v]) => ({[v*10]:k})),
);
seq(flip, {one: 1, two: 2, three: 3});
[Transducer] Create a Sequence Helper to Transduce Without Changing Collection Types的更多相关文章
- [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 ...
- [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 tra ...
- create sequence
create sequence seq_test start with 3 increment by 1 minvalue 1 --范围-(1027 -1) maxvalue 99999999999 ...
- oracle SEQUENCE 创建, 修改,删除
oracle创建序列化: CREATE SEQUENCE seq_itv_collection INCREMENT BY 1 -- 每次加几个 STA ...
- Oracle新表使用序列(sequence)作为插入值,初始值不是第一个,oraclesequence
Oracle新表使用序列(sequence)作为插入值,初始值不是第一个,oraclesequence 使用oracle11g插入数据时遇到这样一个问题: 1 --创建测试表-- 2 CREATE T ...
- msql 实现sequence功能增强
create table sequence ( seq_name VARCHAR(50) NOT NULL COMMENT '序列名称', min_val ...
- oracle之sequence详解
Oracle提供了sequence对象,由系统提供自增长的序列号,每次取的时候它会自动增加,通常用于生成数据库数据记录的自增长主键或序号的地方. sequence的创建需要用户具有create seq ...
- C#/.NET Little Wonders: Use Cast() and OfType() to Change Sequence Type(zz)
Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, ...
- Oracle中sequence的使用方法
在Oracle数据库中,sequence等同于序列号,每次取的时候sequence会自动增加,一般会作用于需要按序列号排序的地方. 1.Create Sequence (注释:你需要有CREATE S ...
随机推荐
- Lvs+heartbeat高可用高性能web站点的搭建
这是我们公司在实际的生产环境当中使用的一套东西,希望对大家有所帮助(实际的公网ip,我已经做了相应的修改): 说明:每台服务器需要有两块网卡:eth0连接内网的交换机,用私网ip,实现服务器间内部访问 ...
- Python 绘图与可视化 matplotlib 制作Gif动图
参考链接:https://blog.csdn.net/theonegis/article/details/51037850 官方文档:https://matplotlib.org/3.1.0/api/ ...
- 小学生绞尽脑汁也学不会的python(初识面对对象)
小学生绞尽脑汁也学不会的python(初识面对对象) 一. 面向对象思想 1. 面向过程. 重点在"过程". 按照实物的发展流程. 先干嘛,后干嘛, 最后干嘛.... 优点: 简单 ...
- 使用Oracle Database Instant Client 精简版
如果只为了在开发环境中访问Oracle,推荐使用Oracle Database Instant Client(精简版)它相对小巧且不需要安装绿色方便移植. 官方下载Instant Client,在Or ...
- 【codeforces 734F】Anton and School
[题目链接]:http://codeforces.com/problemset/problem/734/F [题意] 给你两个数组b和c; 然后让你找出一个非负数组a满足题中所给关系; [题解] 有个 ...
- 【转】Visual Studio單元測試小應用-測執行時間
[转]Visual Studio單元測試小應用-測執行時間 Visual Studio的單元測試會記錄每一個測試的執行時間,如果有幾個Method要測效能,以前我會用Stopwatch,最近我都改用單 ...
- 【转】 c#中两个DateTimePicker,一个时间设置为0:0:0,另一个设置为23:59:59
[转] c#中两个DateTimePicker,一个时间设置为0:0:0,另一个设置为23:59:59 stp1为第一个DateTimePicker this.dtp1.Value=this.dtp1 ...
- l洛谷 P2326 AKN’s PPAP
P2326 AKN’s PPAP 题目描述 “I have a pen,I have an apple.Eh,Apple-Pen!. I have a pen,I have pineapple.En, ...
- windows server 2008开机自动登陆无密码,关机不必写原因
运行secpol.sec接下来,在弹出的“本地安全策略”对话框中,依次展开左边树图到“本地策略”-“安全选项”,在右边可以找到“交互式登录 无须按 Ctrl+Alt+Del”,双击该项设置为“已启用” ...
- doT.js的使用
引言 doT.js可以更好的在html端使用json数据. {{ }} for evaluation 模板标记符 {{= }} for interpolation 输出显示,默认变量名叫it {{! ...