[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.

时间: 2024-11-05 22:41:19

[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 it has the all powerful slice()--and unlike Array it offers functional methods like take() and skip(). You get the best of both the imperative and fu

HTML5 画布上的 Three.js 环境灯光(HTML5 Canvas Three.js Ambient Lighting)

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. HTML5 画布上的 Three.js 环境灯光HTML5 Canvas Three.js Ambient Lighting <!DOCTY

JS中单引号/双引号以及外部js引入的一些问题

一.单引号和双引号的用法的问题 在JavaScript中可以使用单引号.双引号,二者也可以混合使用.但是,身为菜鸟的我,却碰到了一些引号的使用问题. <body> <div style="border:1px solid red; height:150px;width:150px;" onclick="alert("test");" id="test">This is a test. </div&

《Node.js入门》Windows 7下Node.js Web开发环境搭建笔记

近期想尝试一下在IBM Bluemix上使用Node.js创建Web应用程序.所以须要在本地搭建Node.js Web的开发測试环境. 这里讲的是Windows下的搭建方法,使用CentOS 的小伙伴请參考:<Node.js入门>CentOS 6.5下Node.js Web开发环境搭建笔记 Node.js是什么? 我们看看百科里怎么说的? JavaScript是一种执行在浏览器的脚本,它简单,轻巧.易于编辑,这样的脚本通经常使用于浏览器的前端编程.可是一位开发人员Ryan有一天发现这样的前端式

JS中的模块化开发之Sea.JS

模块化开发的好处: 1:减少冲突 2:提高性能 用sea.js为例:sea.js模块库下载地址:http://seajs.org/docs/#downloads 例子:获取非行间样式的模块化开发: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="

iOS JS 交互之利用系统JSContext实现 JS调用oc方法

ios js 交互分为两块: 1.oc调用js 这一块实现起来比较简单, 我的项目中加载的是本地的html,js,css,需要注意的是当你向工程中拖入这些文件时,选择如下操作,(拖入的文件夹是蓝色的,相对路径),不然css,js 的路径会存在问题 加载本地html: oc调用js:一句代码搞定 2.js 调用oc js调用oc又分为两种: 1.js端是直接调用方法 这里就要说到ios7才推出的一个新的api    JavaScriptCore,首先我们引入这个类,并初始化一个JSContext对

js便签笔记(8)——js加载XML字符串或文件

1. 加载XML文件 方法1:ajax方式.代码如下: var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); xhr.open("GET", "data.xml", false); xhr.send(null); var xmlDoc = xhr.responseXML; console.log(xmlDoc

phantomjs helloworld.js报错: Can&#39;t open &#39;helloworld.js&#39;

PhantomJS是一个无界面的,可脚本编程的WebKit浏览器引.它原生支持多种web 标准:DOM 操作,CSS选择器,JSON,Canvas 以及SVG. 当我安装好PhantomJS后,写下第一个js文件,运行发现报错. 我尝试了一些方法:比如将helloworld.js文件所在路径放到环境变量path下,发现这样运行徒劳无功. 后来,在http://bbs.fishc.com/forum.php?mod=viewthread&do=tradeinfo&tid=79336这个网页上

JS 循环遍历JSON数据 分类: JS技术 JS JQuery 2010-12-01 13:56 43646人阅读 评论(5) 收藏 举报 jsonc JSON数据如:{&amp;quot;options&amp;quot;:&amp;quot;[{

JS 循环遍历JSON数据 分类: JS技术 JS JQuery2010-12-01 13:56 43646人阅读 评论(5) 收藏 举报 jsonc JSON数据如:{"options":"[{/"text/":/"王家湾/",/"value/":/"9/"},{/"text/":/"李家湾/",/"valu e/":/"10