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的更多相关文章

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

  2. 由js apply与call方法想到的js数据类型(原始类型和引用类型)

    原文地址:由js apply与call方法想到的js数据类型(原始类型和引用类型) js的call方法与apply方法的区别在于第二个参数的不同,他们都有2个参数,第一个为对象(即需要用对象a继承b, ...

  3. io.js入门(一)—— 初识io.js

    io.js可以说是彻底从NodeJS里分离出来的一条分支,其事情始末可以查看这篇报道,此处便也不赘言.既然是分支,io.js便也基本兼容NodeJS的各种API,连执行指令也依旧兼容Node的 nod ...

  4. js相对路径相关(比如:js中的路径依赖导入该js文件的路径)

    问题描述: 前几天调用同事的js接口文件,在他自己的html测试页面ok,在我这边调用时出现问题. debug过程中,将该测试html移到其他位置都不行,放到原html测试页面同层次路径下是OK的. ...

  5. 【转】关于URL编码/javascript/js url 编码/url的三个js编码函数

    来源:http://www.cnblogs.com/huzi007/p/4174519.html 关于URL编码/javascript/js url 编码/url的三个js编码函数escape(),e ...

  6. App.js – 用于移动 Web App 开发的 JS 界面库

    App.js 是一个轻量级的 JavaScript UI 库,用于创建像本地应用程序的移动 Web 应用而不牺牲性能和体验.它是跨平台的,特定的UI设计,配置类似原生的过渡效果.App.js 的目的是 ...

  7. 一个问题提交的实例(js原生动画,原生ajax,js引用加参数)

    document.writeln("<div id=\"tanchuangwai\" class=\"tanchuangwai\" style= ...

  8. JS一般般的网页重构可以使用Node.js做些什么(转)

    一.非计算机背景前端如何快速了解Node.js? 做前端的应该都听过Node.js,偏开发背景的童鞋应该都玩过. 对于一些没有计算机背景的,工作内容以静态页面呈现为主的前端,可能并未把玩过Node.j ...

  9. Js判断对象是否为空,Js判断字符串是否为空

    Js判断对象是否为空,Js判断字符串是否为空,JS检查字符串是否为空字符串 >>>>>>>>>>>>>>>&g ...

随机推荐

  1. [RxJS] Handling a Complete Stream with Reduce

    When a stream has completed, you often need to evaluate everything that has happened while the strea ...

  2. [RxJS] Reactive Programming - Rendering on the DOM with RxJS

    <!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery- ...

  3. axis WebServices 完美调用天气预报,查询、显示 代码!

    axis WebServices 完美调用天气预报,查询.显示 代码! 效果: jsp页面: <%@ page language="java" import="ja ...

  4. OC-字典&数组运用实例:通讯录的实现

    需求实现: 一.定义联系⼈类ContactPerson 实例变量:姓名.性别.电话号码.住址.分组名称. 方法:初始化⽅方法(姓名.电话号码).显⽰示联系⼈信息 二.定义AddressBook类, 封 ...

  5. C语言随记-1

    涉及指针.数组.函数指针 几种声明形式 int *a[5]; // a是一个有5个元素的数组,每个元素是整数类型指针(int *) int *a[] = {0x100, 0x104, 0x108, 0 ...

  6. ssh命令

    使用ssh命令登陆远程系统 ssh [ip/address] -l [登陆用户名] 如: ssh www.xyz.cn -l root

  7. Sass函数--颜色函数--RGB颜色函数

    RGB颜色函数-RGB()颜色函数 主要分为 RGB , HSL 和 Opacity 三大函数,当然其还包括一些其他的颜色函数,比如说 adjust-color 和 change-color 等.1. ...

  8. jaxb xml to bean

    package www.garbin.com.utils; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException ...

  9. POJ3041 二分图最大匹配

    问题:POJ3041 分析: 构造二分图:令A = B = { 1, 2, ... , n }, 分别代表行号集与列号集.假如第i行第j列有一颗行星,则连接Ai与Bj, 表示必须从Ai(即第i行),B ...

  10. Servlet接收JSP参数乱码问题解决办法

    转自:http://lavasoft.blog.51cto.com/62575/274527/   环境: apache-tomcat-6.0.24.zip jdk1.6.0_16 WindosXP ...