[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 ...
随机推荐
- 如何使用JAVA请求HTTPS
JDK对应的TLS版本(仅供参考) 1.写一个SSLClient类,继承至HttpClient import java.security.cert.CertificateException; impo ...
- NYIST 973 天下第一
天下第一时间限制:1000 ms | 内存限制:65535 KB难度:3 描述AC_Grazy一直对江湖羡慕不已,向往着大碗吃肉大碗喝酒的豪情,但是“人在江湖漂,怎能 不挨刀",”人在江湖身 ...
- CSVHelper读出乱码 解决方案
using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read)) using (StreamRe ...
- windows下使用libsvm3.2
一.官方介绍 libsvm主页:https://www.csie.ntu.edu.tw/~cjlin/libsvm/index.html libsvm介绍文档:http://www.csie.ntu. ...
- 博客迁移到reetsee.com
正如上一篇博客所言.眼下CSDN的博客已经基本完毕它的使命了.感谢CSDN带给我的全部美好回顾. 如今我想尝试一下自己维护一个博客,所以博客的全部内容都迁移到了reetsee.com. 以后博客更新会 ...
- Android中文API-ViewStub
ViewStub控件是一个不可见,0尺寸得惰性控件.当ViewStub控件设置可见,或者调用inflate(),并运行完毕之后,ViewStub所指定的layout资源就会被载入.这个ViewStub ...
- java-面向对象(二)
这几天正在看HeadFirst的时候,突然认为面向对象的几个特点,有点理解模糊不清.所以在这再次回想一下,加深印象. 上篇博客(http://blog.csdn.net/u010539352/arti ...
- c语言中类型转换与赋值运算符、算术运算符、关系运算符、逻辑运算符。原码、反码、补码。小解。
类型转换 自动转换 小范围的类型能够自动转换成大范围的类型.short->int->long->float->double 强制类型转换 (类型名)变量或数值 #include ...
- TSNE——目前最好的降维方法
转自:http://blog.csdn.net/u012162613/article/details/45920827 1.流形学习的概念 流形学习方法(Manifold Learning),简称流形 ...
- Android 自定义View 之利用ViewPager 实现画廊效果(滑动放大缩小)
http://www.2cto.com/kf/201608/542107.html