[Immutable.js] Exploring Sequences and Range() in Immutable.js
Understanding Immutable.js's Map() and List() structures will likely take you as far as you want to go with immutable programming. They have only small semantic differences between each other and the remaining structures in the Immutable.js family. Sequence, however, has one major difference: it's lazy--which opens a new realm of functional possibilities. Let's write a simple sequence to start.
Seq is lazy — Seq does as little work as necessary to respond to any method call. Values are often created during iteration, including implicit iteration when reducing or converting to a concrete data structure such as a List
or JavaScript Array
.
let numbers = Immutable.Range(0, 100); let seq = Immutable.Seq.of(...numbers).take(9); // Seq do nothing now //Use toArray() to actually make it works console.log(seq.toArray()); // [0, 1, 2, 3, 4, 5, 6, 7, 8]
Cache for Seq -- You are able to use .cacheResult() method to cache the Seq:
it('should cache results of Seq()', () => { let objects = Immutable.Range(0, 1000).map(() => { return new Object(); }); let take100 = objects.take(100).toArray(); let take100Again = objects.take(100).toArray(); take100.forEach((obj, index) => { expect(obj === take100Again[index]).to.be.false; }) let cachedObjects = Immutable.Range(0, 1000).map(() => { return new Object(); }).cacheResult(); expect(cachedObjects.size).to.equal(1000); let take100Cached = cachedObjects.take(100).toArray(); let take100CachedAgain = cachedObjects.take(100).toArray(); take100Cached.forEach((obj, index) => { expect(obj === take100CachedAgain[index]).to.be.true; }) });
Example shows each time Seq runs will create a new objects, so if you compare 'take100' and 'take100Again', they are different object, because everytime go thoguth the Seq, it will create a new object.
But when you apply cache, the 'take100Cached' and 'take100CachedAgain' they are the same.
var squares = Immutable.Seq.of(1,2,3).map(x => {console.log("here");return x * x}); var res = squares.join() + squares.join();
If see the console.log(); there are six times "here";
var squares = Immutable.Seq.of(1,2,3).map(x => {console.log("here");return x * x}).cacheResult(); var res = squares.join() + squares.join();
When cache applies, only console.log three times.
相关文章
- Fabric.js 使用纯色遮挡画布(前景色)
- js 字符串转数组,js 字符串截取
- node.js(5) MySQL数据库的学习
- 【说站】js如何实现类型判断
- js定时器与延时器_JS做定时器倒计时
- vue.js客服系统实时聊天项目开发(二十二)vue项目中router.js路由介绍
- 引入JS文件和JQuery
- 纯CSS显示JS/HTML等源代码行数
- Js删除字符串中的指定字符串详解编程语言
- 搜索MySQL中IN搜索的应用(mysql包含in)
- 语法应用Oracle中IN语法的多种方式(oracle中in)
- 使用Node.js连接MySQL数据库实现数据交互(mysqlnodejs)
- 深入Linux内核:IN后缀操作系统之旅(linux系统in后缀)
- 查询解锁Oracle多条件In查询的机密(oracle多条件in)
- 如何有效运用SQL Server的IN语句(sqlserver用in)
- MySQL中IN语句的限制详解(mysql中in的限制)
- MySQL中使用IN操作符查询字符串(mysql中in字符串)
- 优化MySQL查询避免使用IN关键字(mysql 不用 in)
- 利用Oracle中的IN语句优化查询效率(oracle中的in语句)
- 限制解除Oracle中IN语句数量限制(oracle中in的数量)
- 的区别Oracle中IN与的异同探究(oracle中in和=)
- 查询Oracle IN查询两个字段的精彩之处(oracle两个字段in)
- 从Oracle到NO IN数据库迁移指南(oracle no in)
- 提升Oracle IN操作中的性能改进(oracle in的性能)
- jqueryvalidate.js表单验证的基本用法入门
- js优化次数过多的循环考虑到性能问题
- 远离JS灾难css灾难之js私有函数和css选择器作为容器
- 把大数据数字口语化(python与js)两种实现
- js字符串转换成数字与数字转换成字符串的实现方法
- js获取通过ajax返回的map型的JSONArray的方法
- JS操作CSS随机改变网页背景实现思路
- js分页工具实例