博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Transducer] Lazyness in Transduer
阅读量:5903 次
发布时间:2019-06-19

本文共 1124 字,大约阅读时间需要 3 分钟。

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 s 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(),);

 

转载地址:http://fhkpx.baihongyu.com/

你可能感兴趣的文章
网络最大流问题算法小结 [转]
查看>>
iOS推送消息报错误“Domain=NSCocoaErrorDomain Code=3000”的可能问题
查看>>
kvm-1
查看>>
leetcode 64. Minimum Path Sum
查看>>
textkit
查看>>
CentOS7+CDH5.14.0安装CDH错误排查: HiveServer2 该角色的进程已退出。该角色的预期状态为已启动...
查看>>
The Oregon Trail 俄勒冈之旅
查看>>
Excel VBA连接MySql 数据库获取数据
查看>>
Developing a Service Provider using Java API(Service Provider Interface)(转)
查看>>
BAE Flask UEditor 使用七牛云
查看>>
Bootstrap系列 -- 15. 下拉选择框select
查看>>
关于WinPE安装操作系统
查看>>
LeetCode Median of Two Sorted Arrays
查看>>
oschina程序开发
查看>>
mysql创建每月执行一次的event
查看>>
ReactNative常用组件汇总
查看>>
nested exception is java.lang.NoClassDefFoundError: net/sf/cglib/proxy/CallbackFilter
查看>>
“正在注册字体”问题解决
查看>>
windows10 更新后要输入2次密码才能进入系统
查看>>
iOS开发-OpenGL ES入门教程1
查看>>