[Transducer] Lazyness in Transduer
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的更多相关文章
- Scalaz(48)- scalaz-stream: 深入了解-Transducer: Process1-tee-wye
在上一篇讨论里我们介绍了Source,它的类型款式是这样的:Process[F[_],O].Source是通过await函数来产生数据流.await函数款式如下: def await[F[_], A, ...
- [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 ...
- [Transducer] Step by Step to build a simple transducer
Transducers are composable algorithmic transformations. They are independent from the context of the ...
- 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 ...
- [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 ...
- [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 ...
- 翻译连载 | 附录 A:Transducing(下)-《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇
原文地址:Functional-Light-JS 原文作者:Kyle Simpson-<You-Dont-Know-JS>作者 关于译者:这是一个流淌着沪江血液的纯粹工程:认真,是 HTM ...
- WATERHAMMER: A COMPLEX PHENOMENON WITH A SIMPLE SOLUTION
开启阅读模式 WATERHAMMER A COMPLEX PHENOMENON WITH A SIMPLE SOLUTION Waterhammer is an impact load that is ...
- ElasticSearch详解与优化设计
简介 概念 安装部署 ES安装 数据索引 索引优化 内存优化 1简介 ElasticSearch(简称ES)是一个分布式.Restful的搜索及分析服务器,设计用于分布式计算:能够达到实时搜索,稳定, ...
随机推荐
- code-reading-notes--xml 解析
- Myeclipse关闭JS等文件的验证
点击 window > 右键单击properties,弹出properties界面 然后选择MyEclipse->validation->Excluded Resource下找到不需 ...
- thinkPHP利用ajax异步上传图片并显示、删除
近来学习tp5的过程中,项目中有个发帖功能,选择主题图片.如下: 利用原始的文件上传处理,虽然通过原始js语句能实时显示上传图片,但是这样的话会涉及很多兼容问题.使用ajax技术,实现选择性删除所选图 ...
- [luogu] P2354 [NOI2014]随机数生成器 (贪心)
Description Input 第1行包含5个整数,依次为 x_0,a,b,c,d ,描述小H采用的随机数生成算法所需的随机种子.第2行包含三个整数 N,M,Q ,表示小H希望生成一个1到 N×M ...
- a.WHERE使用中单行子查询(适用于>,<,=,>=,<=等条件)
a.单行子查询(适用于>,<,=,>=,<=等条件) //查询工资最高的员工编号和员工名 select empno,ename from emp where ...
- debian mysql 定时自己主动备份的脚本
#!/bin/sh LOG=/var/log/mysql-backup.log # mysql db info USER_ROOT=XXXXXX USER_PWD=XXXXXXX # mysql da ...
- 17、lambda表达式
一.简介 lambda表达式允许你通过表达式来代替功能接口,lambda表达式就和方法一样,它提供了一个正常的参数列表和一个使用这些参数的主体(body,可以是一个表达式或一个代码块),它还增强了集合 ...
- Binary Tree Inorder Traversal--leetcode
原题链接:https://oj.leetcode.com/problems/binary-tree-inorder-traversal/ 题目大意:中序遍历二叉树 解题思路:中序遍历二叉树.中序遍历二 ...
- systemd服务管理---systemctl命令列出所有服务
1.列出系统所有服务 #systemctl list-units --all --type=service
- [雅礼NOIP2018集训 day4]
感觉状态极差啊,今天居然爆零了 主要是以下原因: 1.又是T1看错题肝了两个小时,发现题意理解错误瞬间心态爆炸 2.T2交错了文件名 3.T3暴力子任务和正解(假的)混在一起,输出了两个答案 都想为自 ...