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. PHP实现的毫秒定时器,同时解决进程不重复堆积

    定时器任务,在WEB应用比较常见,如何使用PHP实现定时器任务,大致有两种方案:1)使用Crontab命令,写一个shell脚本,在脚本中调用PHP文件,然后定期执行该脚本:2)配合使用ignore_ ...

  2. vue如何根据返回的值对元素进行样式渲染

    1.最终显示样式: 需要:根据任务状态值,显示不同颜色的原点表示任务状态,以及对优先级的数据,进行☆标记 2.代码实现: 在<el-table-column>中需要显示的内容前面,添加图标 ...

  3. crm 系统项目(一) 登录,注册,校验

    crm 系统项目(一) 登录,注册,校验 首先创建一个Django项目,关于配置信息不多说,前面有~ models.py文件下创建需要的表格信息,之后导入数据库 from django.db impo ...

  4. IIC 原理讲解

    IIC具体是什么这里我就不细说了,只收集一些关于IIC的原理. IIC总线优点是节约总线数,稳定,快速, 是目前芯片制造上非常 流行的一种总线,大多数单片机已经片内集成了IIC总线接口,无 需用户自己 ...

  5. cocos2d-x学习笔记(18)--游戏打包(windows平台)

    cocos2d-x学习笔记(18)--游戏打包(windows平台)           之前做好的游戏,都是在vs2008下编译执行的.假设说想把游戏公布到网上或者和其它人一起分享游戏,那就得对游戏 ...

  6. ZOJ 3329

    方程很明显有 d[i]=sum(pk*d[i+k])+p0*d[0]; 其中pi可以在开始时枚举求出. 设d[i]=A[i]*d[0]+B[i], 代入上式 d[i]=(sum(pk*A[i+k])+ ...

  7. 赵雅智_使用SQLiteDatabase操作SQLite数据库及事务

    知识点具体解释:http://blog.csdn.net/zhaoyazhi2129/article/details/9025995 详细代码: MainActivity.java package c ...

  8. 【万里征程——Windows App开发】控件大集合2

    以下再来看看一些前面还没有讲过的控件,只是控件太多以至于无法所有列出来,大家仅仅好举一反三啦. Button 前面最经常使用的控件就是Button啦,Button另一个有意思的属性呢.当把鼠标指针放在 ...

  9. hdu5351

    题目名称:MZL's Border 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5351 题意:给出fib 1 = b,fib2 = a ; fib  ...

  10. shadowOffset 具体解释

    x向右为正,y向下为正 1.y<0 UILabel *label=[[UILabelalloc] initWithFrame:CGRectMake(40,40, 250,50)]; label. ...