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. QT实现透明效果的按钮

    QPushButton { color: rgb(0,88,152) background-color: rgba(97%,80%,9%,50%)}

  2. [Android算法] bitmap 将图片压缩到指定的大小

    Bitmap压缩到指定大小: private void imageZoom() {//图片允许最大空间 单位:KBdouble maxSize =400.00;//将bitmap放至数组中,意在bit ...

  3. display:inline和display:inline-block的区别

    先来一张图: 测试代码: <!DOCTYPE html> <html> <head> <style> #bb { overflow: hidden; b ...

  4. Combination Sum,Combination Sum II,Combination Sum III

    39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique co ...

  5. 引用 exit、return、_exit、_Exit这几个函数的区别

    引用 exit.return._exit._Exit这几个函数的区别 一.exit函数和return函数的主要区别是: exit用于在程序运行的过程中随时结束程序,其参数是返回给OS的.也可以这么讲: ...

  6. iscroll源码初涉

    最近尝试做web app时候,用上了神器iScroll,鉴于功力尚浅,并没有完全用好神器,所以今天特意来认真学习! 翻开官网,目前的版本是5,但是相关的文章并不多,具体的文件版本是: iscroll. ...

  7. JavaScript 语法陷阱

    没有一门编程语言是完美的,JavaScript 也不例外,它语法陷阱重重,防不胜防: 加号 "with" 分号自动插入 声明提升 "eval" 多行字符串 变量 ...

  8. mysql(mariadb)重装

    MariaDB是MySQL的一个分支,主要由开源社区进行维护和升级,而MySQL被Oracle收购以后,发展较慢.在CentOS 7的软件仓库中,将MySQL更替为了MariaDB.  Centos  ...

  9. 基于ViewPager的一些酷炫切换效果

    1.ViewPager可以用于实现类似banner的功能,我曾经在“时间超市”项目中使用过.但如何在此基础上实现一些切换的酷炫效果呢?今天细细品读了鸿洋大神的相关博文,终于学会了如何自定义切换效果. ...

  10. bower解决js库的依赖管理

    从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的Javascript引擎.chrome浏 ...