[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 ...
随机推荐
- 【BZOJ 1486】 [HNOI2009]最小圈
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 我们可以只想那个均值最小的环. 我们不知道那个环由哪些边构成 但我们可以把每条边都减掉mid 那个环受到的影响是什么呢? 如果这个均 ...
- 极路由4pro(HC5962)设置阿里云DDNS
v2ex有个帖子说用Dnspod的API可以一行搞定,不过我既然买的是阿里云的域名还是想尽量用阿里云的API,感觉比较安全,另外修改解析记录后也会自动发邮件通知,所以还是调用阿里云的API吧.阿里云的 ...
- HDU 4316 Contest 2
三个摄像头,在XOY上与立体的点求出在平面上的交点,然后求出凸包.三个凸包相交的面积即是所求,即是可以用半平面交的方法求解了. 模板题了.代码拿别人的. #include<cmath> # ...
- Mysql数据库事务的隔离级别和锁的实现原理分析
Mysql数据库事务的隔离级别和锁的实现原理分析 找到大神了:http://blog.csdn.net/tangkund3218/article/details/51753243 InnoDB使用MV ...
- android:px,dp(dip),sp的差别
1.px:表示屏幕的实际像素,比如320*480的屏幕在横向有320个像素,在纵向有480个像素,假设指定的某个空间的单位为px.那么在不同分辨率下的手机上.显示的都是指定的大小.一般不推荐使用px. ...
- UE4在VS2013中各个编译配置代表意义
UE4中有个各式各样的编译配置,都怎么个意思呢? 对原文的理解和翻译. https://docs.unrealengine.com/latest/INT/Programming/Development ...
- 计算机网络 4.网络层与IP协议
网络中的每一台主机和路由器都有一个网络层部分.而路由器中也没有网络层以上的层次.网络层是协议栈中最复杂的层次. 转发forwarding:当一个分组到达某路由器的输入链路时.该路由器将分组移动到适当的 ...
- 90.bower解决js的依赖管理
转自:https://blog.csdn.net/u011537073/article/details/52951122 前言一个新的web项目开始,我们总是很自然地去下载需要用到的js类库文件,比如 ...
- Redis-1-安装
Redis-1-安装 标签(空格分隔): linux,redis 下载 cd /usr/local/src/ wget http://download.redis.io/releases/redis- ...
- Java Swing 托盘闪烁Demo实例
桌面程序当中,托盘显示是一个常用的功能. C#的WinForm程序要实现托盘是挺轻松的,而Java Swing要实现这个功能虽然也不难, 但是由于Java Swing开发者或许没有C#多,所以看到很多 ...