Transducers remove the requirement of being lazy to optimize for things like take(10). However, it can still be useful to "bind" a collection to a set of transformations and pass it around, without actually evaluating the transformations.

As noted above, whenever you apply transformations to an iterator it does so lazily. It's easy convert array transformations into a lazy operation, just use the utility function iterator to grab an iterator of the array instead.

The whole point here is using 'iterator' from transducer.js lib. So you get control when you need the data.

import t from 'transducers.js';

const doubleTheNumber = number => number * 2;
export const evenOnly = number => number % 2 === 0; const doubleAndEven = t.compose(
t.filter(evenOnly),
t.map(doubleTheNumber),
); const arr = [1,2,3,4,5,6,7,8,9,10];
const res = t.seq(
t.iterator(arr),
t.compose(doubleAndEven, t.take(2)),
); function* makeNumbers() {
let num = 1;
while (true) yield num++;
} const lazyNums = t.seq(makeNumbers(), doubleAndEven); console.log(
lazyNums.next(),
lazyNums.next(),
lazyNums.next(),
lazyNums.next(),
lazyNums.next(),
lazyNums.next(),
);

[Transducer] Lazyness in Transduer的更多相关文章

  1. Scalaz(48)- scalaz-stream: 深入了解-Transducer: Process1-tee-wye

    在上一篇讨论里我们介绍了Source,它的类型款式是这样的:Process[F[_],O].Source是通过await函数来产生数据流.await函数款式如下: def await[F[_], A, ...

  2. [Transducer] Make Transducer works for Iteratable collection and Object

    We've seen how we can transduce from arrays or other iterables, but plain objects aren't iterable in ...

  3. [Transducer] Step by Step to build a simple transducer

    Transducers are composable algorithmic transformations. They are independent from the context of the ...

  4. A Go library implementing an FST (finite state transducer)——mark下

    https://github.com/couchbaselabs/vellum Building an FST To build an FST, create a new builder using ...

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

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

  7. 翻译连载 | 附录 A:Transducing(下)-《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇

    原文地址:Functional-Light-JS 原文作者:Kyle Simpson-<You-Dont-Know-JS>作者 关于译者:这是一个流淌着沪江血液的纯粹工程:认真,是 HTM ...

  8. WATERHAMMER: A COMPLEX PHENOMENON WITH A SIMPLE SOLUTION

    开启阅读模式 WATERHAMMER A COMPLEX PHENOMENON WITH A SIMPLE SOLUTION Waterhammer is an impact load that is ...

  9. ElasticSearch详解与优化设计

    简介 概念 安装部署 ES安装 数据索引 索引优化 内存优化 1简介 ElasticSearch(简称ES)是一个分布式.Restful的搜索及分析服务器,设计用于分布式计算:能够达到实时搜索,稳定, ...

随机推荐

  1. [LeetCode] 242. 有效的字母异位词 valid-anagram(排序)

    注意这里字母异位词的定义是:字母类别及个数都要一样,只是排列顺序不同. class Solution(object): def isAnagram(self, s, t): ""& ...

  2. JavaScript 的对象继承方式,有几种写法?

    JavaScript 的对象继承方式,有几种写法? 一.对象冒充 其原理如下:构造函数使用 this 关键字给所有属性和方法赋值(即采用类声明的构造函数方式).因为构造函数只是一个函数,所以可使 Pa ...

  3. oracle 控制语句

    PL输出语句 set serverout on; -- 开启PL的输出语句功能declare n number:=1; -- 声明一个number型的变量n,并赋值为1 v varchar2(20): ...

  4. ASP.NET-post、get的区别

    post.get的区别 1.get通过把参数加在浏览器的地址栏中提交(最大2K),用post可以进行文件的提交: 2.使用post提交的页面在点击[刷新]按钮的时候浏览器一般会提示"是否重新 ...

  5. HDU 4336

    概率DP期望,逆推即可.使用状态压缩. 注意,要全部输出...看DIS才发现题目输出是个坑.. #include <iostream> #include <cstdio> #i ...

  6. MySQL高可用系列之MHA(二)

    一.參数说明 MHA提供了一系列配置參数.深入理解每一个參数的详细含义,对优化配置.合理使用MHA非常重要.非常多高可用性也都是通过合理配置一些參数而实现的. MHA包含例如以下配置參数,分别说明例如 ...

  7. [Angular] Provide Feedback to Progress Events with Angular’s HttpRequest Object

    In some cases your application might need to upload large amounts of data, such as files. Obviously ...

  8. linux下通过命令启动多个终端运行对应的命令和程序

        作者:张昌昌 在一些情况下,往往须要同一时候启动多个终端并让终端运行自己主动运行对应的命令,进而达到提高操作效率的目的.在linux下gnome-terminal启动终端命令, gnome-t ...

  9. FreeRTOS系列第13篇---FreeRTOS内核控制

    内核控制的一些功能须要移植层提供,为了方便移植.这些API函数用宏来实现,比方上下文切换.进入和退出临界区.禁止和使能可屏蔽中断.内核控制函数还包含启动和停止调度器.挂起和恢复调度器以及用于低功耗模式 ...

  10. 公布自己的pods到CocoaPods trunk 及问题记录

    这两天准备把之前写的一些小玩意加入到pods库中去,參考了一些资料后进行操作,实际中也遇到了一些问题,记录下来.问题及解决方案在后面. 參考内容转载例如以下: 首先更新了用trunk之后,CocoaP ...