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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. create sequence

    create sequence seq_test start with 3 increment by 1 minvalue 1  --范围-(1027 -1) maxvalue 99999999999 ...

  4. oracle SEQUENCE 创建, 修改,删除

    oracle创建序列化: CREATE SEQUENCE seq_itv_collection            INCREMENT BY 1  -- 每次加几个              STA ...

  5. Oracle新表使用序列(sequence)作为插入值,初始值不是第一个,oraclesequence

    Oracle新表使用序列(sequence)作为插入值,初始值不是第一个,oraclesequence 使用oracle11g插入数据时遇到这样一个问题: 1 --创建测试表-- 2 CREATE T ...

  6. msql 实现sequence功能增强

    create table sequence (      seq_name        VARCHAR(50)  NOT NULL COMMENT '序列名称',    min_val        ...

  7. oracle之sequence详解

    Oracle提供了sequence对象,由系统提供自增长的序列号,每次取的时候它会自动增加,通常用于生成数据库数据记录的自增长主键或序号的地方. sequence的创建需要用户具有create seq ...

  8. 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, ...

  9. Oracle中sequence的使用方法

    在Oracle数据库中,sequence等同于序列号,每次取的时候sequence会自动增加,一般会作用于需要按序列号排序的地方. 1.Create Sequence (注释:你需要有CREATE S ...

随机推荐

  1. Laravel核心解读--Contracts契约

    Contracts Laravel 的契约是一组定义框架提供的核心服务的接口, 例如我们在介绍用户认证的章节中到的用户看守器契约IllumninateContractsAuthGuard 和用户提供器 ...

  2. STM32 抢占优先级和响应优先级

    一.抢占优先级和响应优先级 STM32 的中断向量具有两个属性,一个为抢占属性,另一个为响应属性,其属性编号 越小,表明它的优先级别越高. 抢占,是指打断其他中断的属性,即因为具有这个属性会出现嵌套中 ...

  3. 【2018 Multi-University Training Contest 2 1007】Naive Operations

    [链接] 我是链接,点我呀:) [题意] 给你两个数组a,b; b数组是1..n的一个排列. 现在给你两种操作: add l,r将a[l..r]都加上1 query l,r 询问$∑^r_l\frac ...

  4. ZOJ 2705

    这题,找找规律,可以发现一个斐波那契数列.按照斐波那契数列求和,知道, SUM=Fn+2-F1,于是,该长度为Fn+2的倍数.因为斐波那契数列不一定是从1开始的,而从2开始的每个数都是从1开始的倍数. ...

  5. python学习之--安装IDE(eclipse+pydev)

    First steps download eclipse url:http://www.eclipse.org/downloads/ select Help -> Install New Sof ...

  6. centos6.5配置SSH免password登录

    创建新用户:useradd hadoop 设置password:passwd hadoop,输入自己想要的password就可以.之后su hadoop切换用户 改动主机名:vim /etc/sysc ...

  7. GCC 优化选项 -O1 -O2 -O3 -OS 优先级,-FOMIT-FRAME-POINTER(O3的优化很小,只增加了几条优化而已)

    四种编译优化类型的解释: `-O ' `-O1 '                 Optimize.      Optimizing   compilation   takes   somewhat ...

  8. caffe环境配置2

    参考链接: http://blog.csdn.net/enjoyyl/article/details/47397505 http://blog.csdn.net/baobei0112/article/ ...

  9. 如何将MVC AREA中的某一个页设为起始页

    public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.Ignore ...

  10. VSCode向上的代码提示消除

    VSCode虽然好用, 但是有些用户体验实在非常差, 比如这种往上面弹的类型提示... 在用户设置中增加: "editor.parameterHints": false