[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 ...
随机推荐
- C++11 多线程 基础
C++11开始支持多线程编程,之前多线程编程都需要系统的支持,在不同的系统下创建线程需要不同的API如pthread_create(),Createthread(),beginthread()等,使用 ...
- Div与table的区别
1:速度和加载方式方面的区别 div 和 table 的差异不是速度,而是加载方式,速度只能是指网络速度,如果速度足够快,是没有差异的: div 的加载方式是即读即加载,遇到 <div> ...
- C#Equal的使用
代码如下: public partial class FramMain : Form { public FramMain() { InitializeComponent(); } private vo ...
- UIApplication的作用
1.设置app图标右上角的数字2.设置状态栏的属性(样式.是否要显示)3.打开某个链接\发短信\打电话4.keyWindow : 访问程序的主窗口(一个程序只能有一个主窗口)5.windows : 访 ...
- asp Eval()函数的一些使用总结
一.函数的原型: Eval(string s); s可以是变量,表达式,语句: 二.使用: 1.s为变量时,返回该变量的值,如:string s = "sss";Eval(s);/ ...
- highcharts的使用
步骤: 1. 去highcharts官网下载最新版本 2. 在.aspx页面添加引用 例: <link href="../JS/highcharts/css/highslide.css ...
- android 隔几秒再执行
今天做项目,需要前面的方法执行完等待2秒在关闭当前页面.之前使用的是Thread.sleep(2000)发现根本没有作用.经过多次尝试,发现需要使用以下方法才能实现: new Thread(){ pu ...
- silverlight visifire控件图表制作——silverlight 后台方法ControlChart.xaml.cs
一.构造方法ControlChart 1.前台页面控件赋值 //时间下拉框赋值,下拉框赋选定值 for (int ii = DateTime.Today.Year; ii ...
- php+mysql将大数据sql文件导入数据库
<?php $file_name = "d:test.sql"; $dbhost = "localhost"; $dbuser = "root& ...
- jQuery中的插件的编写和使用
1,常用的jQuery插件:表单验证插件formValidator a.目前支持5大验证方式:1.inputValidator(针对input,textarea,select控件的字符长度,值范围,选 ...