[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.
[Immutable.js] Exploring Sequences and Range() in Immutable.js的更多相关文章
- [Immutable.js] Working with Subsets of an Immutable.js Map()
Immutable.js offers methods to break immutable structures into subsets much like Array--for instance ...
- 由js apply与call方法想到的js数据类型(原始类型和引用类型)
原文地址:由js apply与call方法想到的js数据类型(原始类型和引用类型) js的call方法与apply方法的区别在于第二个参数的不同,他们都有2个参数,第一个为对象(即需要用对象a继承b, ...
- io.js入门(一)—— 初识io.js
io.js可以说是彻底从NodeJS里分离出来的一条分支,其事情始末可以查看这篇报道,此处便也不赘言.既然是分支,io.js便也基本兼容NodeJS的各种API,连执行指令也依旧兼容Node的 nod ...
- js相对路径相关(比如:js中的路径依赖导入该js文件的路径)
问题描述: 前几天调用同事的js接口文件,在他自己的html测试页面ok,在我这边调用时出现问题. debug过程中,将该测试html移到其他位置都不行,放到原html测试页面同层次路径下是OK的. ...
- 【转】关于URL编码/javascript/js url 编码/url的三个js编码函数
来源:http://www.cnblogs.com/huzi007/p/4174519.html 关于URL编码/javascript/js url 编码/url的三个js编码函数escape(),e ...
- App.js – 用于移动 Web App 开发的 JS 界面库
App.js 是一个轻量级的 JavaScript UI 库,用于创建像本地应用程序的移动 Web 应用而不牺牲性能和体验.它是跨平台的,特定的UI设计,配置类似原生的过渡效果.App.js 的目的是 ...
- 一个问题提交的实例(js原生动画,原生ajax,js引用加参数)
document.writeln("<div id=\"tanchuangwai\" class=\"tanchuangwai\" style= ...
- JS一般般的网页重构可以使用Node.js做些什么(转)
一.非计算机背景前端如何快速了解Node.js? 做前端的应该都听过Node.js,偏开发背景的童鞋应该都玩过. 对于一些没有计算机背景的,工作内容以静态页面呈现为主的前端,可能并未把玩过Node.j ...
- Js判断对象是否为空,Js判断字符串是否为空
Js判断对象是否为空,Js判断字符串是否为空,JS检查字符串是否为空字符串 >>>>>>>>>>>>>>>&g ...
随机推荐
- UIActivityIndicatorView-初识IOS
UIActivityIndicatorView是一个加载动画的视图,一般加载一个网页页面之前会经常用到. 上一个随笔,我讲到了页面加载的页面的那些代理方法 - (void) viewWillAppea ...
- AngularJs练习Demo9 Http
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...
- silverlight .net后台 设置visifire控件图表样式 属性说明
.net后台 代码: 如图 Chart chart = new MyCharts(); //图表 //chart.Watermark = false; //没好使 ...
- 浅谈Windows Server APPFABRIC
hi,everyone !真的是好久好久没有update blog了,因为最近忙着备考,没有时间对<数据结构与算法>进行研究学习了.所以,blog一直未更新.today is Friday ...
- Find The Multiple
算法:深搜(水题): Given a positive integer n, write a program to find out a nonzero multiple m of n whose d ...
- Reverse digits of an integer.
class Solution { public: int reverse(int x) { ;//long 是怕中间过程溢出 <<,max=-min-){ ans=ans*+x%; x=x ...
- Oracle11g R2学习系列 之五回闪
Oracle里面有一个回闪的操作,这个貌似sql server是没有的.要使用这个功能,需要用到两个时间内部函数 TIMESTAMP和TO_TIMESTAMP.其中,函数TO_TIMESTAMP的语法 ...
- 调查程序phpquestionnaire 2.2中文安装注意
最近找调查投票软件 找到limesurvey 和 phpquestionnaire,limesurvey 中文可以用,但是比较烦琐.phpquestionnaire有汉化过的包下载,但是安装完,发现中 ...
- python保留指定文件、删除目录其他文件的功能(1)
由于给客户的发布版本上客户改动了些代码和图片,我们这边给他们更新publish都是增量更新(开发提供更新指定的文件,我们提取出来给客户进行覆盖更新),但有时需要更新的文件较多导致不得不一个一个的进行查 ...
- 论如何进CSDN博客排名前500
http://www.jtahstu.com/blog/post-71.html 目前该方法并不适用于博客园,显然写博客园的程序员智商要高些.