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. IE6 浏览器常见兼容问题 共23个

    1.<!DOCTYPE HTML>文档类型的声明. 产生条件:IE6浏览器,当我们没有书写这个文档声明的时候,会触发IE6浏览器的怪异解析现象: 解决办法:书写文档声明. 2.不同浏览器当 ...

  2. 王立平-bmp.compress()

    bmp.compress(Bitmap.CompressFormat.JPEG, 30, baos); //30 是压缩率,表示压缩70%; 假设不压缩是100,表示压缩率为0

  3. Android多媒体学习六:利用Service实现背景音乐的播放

    Android同意我们使用Service组件来完毕后台任务.这些任务的同意不会影响到用户其它的交互. 1.Activity类 [java] view plaincopy package demo.ca ...

  4. IOS假设将一个十六进制的color转换成UIColor,非常有用

    UI给开发的效果图非常多时候标注着十六进制的Color,而程序中用到的往往是UIColor能够用例如以下方法去转换: (UIColor *)RGBColorFromHexString:(NSStrin ...

  5. Leetcode_num1_Single Number

    好久没有做题啦.从今天開始刷Leetcode的题.希望坚持的时间能长一点. 先从ac率最高的Single Number開始吧. 题目: Given an array of integers, ever ...

  6. Android px,dp,pt,sp的差别

    px(像素点) mm 等Android不建议用 为什么电脑web开发能够用而Android不建议用? 由于px代表像素点个数,一般电脑分辨率都同样 不管14寸还是15寸都是1366*768而手机分辨率 ...

  7. java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start com

    错误如题. 原因:web.xml中的servlet映射<url-pattern> 配置错误 改动正确就可以. 我直接删除了,bug就攻克了. 另一个问题是 xxx.jar fail to ...

  8. zzuoj--10399--Turing equation(模拟)

    Turing equation Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 152  Solved: 85 [Submit][Status][Web ...

  9. zzuoj--10424--无聊的课(简单几何)

    10424: 无聊的课 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 81  Solved: 16 [Submit][Status][Web Boar ...

  10. 13.boost有向无向图邻接表表示

    #include <iostream> #include <boost/config.hpp> //图(矩阵实现) #include <boost/graph/adjac ...