We usually think of types as something that can define a single layer of an object: with an interface we normally specify a list of a few properties and their respective types. If any one of those properties is another object we must refer again to its type. This is a finite process, as eventually we will get to a flat object, that doesn’t reference any other objects. Trees and Linked Lists are dynamic data structures, that can have infinitely many levels of depth. A type alias in TypeScript can use generics and refer to itself - this can be used to create these potentially infinitely long data structures. We will also explore using Linked Lists for building a Redux time travelling debugger.

For example:

interface TreeNode<T> {
value: T;
left: TreeNode<T>;
right: TreeNode<T>;
} interface LinkedListNode<T> {
value: T;
next: LinkedListNode<T>;
}

While most types have a finite structure, these types can potentially grow infinitely.

let node: LinkedListNode<string>;
node.next.next.next.next.next.next.next.next.value;

Traversing items of custom data structures, like trees or linked lists, require knowledge of how that data structure is built. That can lead to problems, as faulty iteration strategies might not visit all the items, or they might not know when they've finished visiting all of them. In this lesson, we're going to look at how TypeScript supports us in building custom ES6 iterators that can be then used by a simple "for..of" loop to ensure we provide an easy to use and reliable API for other developers to traverse our data structures.

class BackwardsActionIterator implements IterableIterator<Action> {
constructor(private_currentActionNode: ListNode<Action>){ }
[Symbol.iterator](): IterableIterator<Action> {
return this;
} next(): IteratorResult<Action> {
const curr = this._currentActionNode;
if(!curr || !curr.value) {
return {value: null, done: true};
}
//1. move through each item in the list
this._currentActionNode = curr.prev;
//2. return each item
return {value: curr.value, done: false};
}
}

Now we can add some Actions:

let action1 = { type: "LOGIN" };
let action2 = { type: "LOAD_POSTS" };
let action3 = { type: "DISPLAY_POSTS" };
let action4 = { type: "LOGOUT" }; let actionNode1: ListNode<Action> = {
prev: null,
next: null,
value: action1
}; let actionNode2: ListNode<Action> = {
prev: null,
next: null,
value: action2
}; actionNode1.next = actionNode2; let actionNode4: ListNode<Action> = {
prev: actionNode3,
next: null,
value: action4
};
actionNode3.next = actionNode4;

Now all we need to do is just create the for of loop. I'm going to take each action one by one and I'm going to go for my backwardsActionsList. On each iteration I just want to print out the type of the action.

const backwardsActionsList = new BackwardsActionIterator(
actionNode4
); for(let action of backwardsActionsList) {
console.log(action.type);
}

RUN:

tsc iterator.ts --target es6

[TypeScript] Custom data structures in TypeScript with iterators的更多相关文章

  1. A library of generic data structures

    A library of generic data structures including a list, array, hashtable, deque etc.. https://github. ...

  2. The Swiss Army Knife of Data Structures … in C#

    "I worked up a full implementation as well but I decided that it was too complicated to post in ...

  3. 剪短的python数据结构和算法的书《Data Structures and Algorithms Using Python》

    按书上练习完,就可以知道日常的用处啦 #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving wit ...

  4. Persistent Data Structures

    原文链接:http://www.codeproject.com/Articles/9680/Persistent-Data-Structures Introduction When you hear ...

  5. Go Data Structures: Interfaces

    refer:http://research.swtch.com/interfaces Go Data Structures: Interfaces Posted on Tuesday, Decembe ...

  6. Choose Concurrency-Friendly Data Structures

    What is a high-performance data structure? To answer that question, we're used to applying normal co ...

  7. 无锁数据结构(Lock-Free Data Structures)

    一个星期前,我写了关于SQL Server里闩锁(Latches)和自旋锁(Spinlocks)的文章.2个同步原语(synchronization primitives)是用来保护SQL Serve ...

  8. [CareerCup] 10.2 Data Structures for Large Social Network 大型社交网站的数据结构

    10.2 How would you design the data structures for a very large social network like Facebook or Linke ...

  9. Manipulating Data Structures

    Computer Science An Overview _J. Glenn Brookshear _11th Edition We have seen that the way data struc ...

随机推荐

  1. For循环中不可以嵌套RDD操作

    今天犯了一个致命理解错误,Spark中的RDD Map操作只是一个计算式的传递,并不是Action,也就是在for循环中不会产生真正的计算. 因此,如果for循环中出现了RDD的Map类似操作,都会引 ...

  2. JSTL c:url

    c:url 标签 jstl 实例代码和用法.     <c:url>标记格式化成一个字符串格式的URL,并将其存储到变量中.这个标签会在必要时自动执行URL重写. var属性指定的变量将包 ...

  3. (5)python 字符串和输入输出

    一.字符串转义 字符串可以包含任何字符可以用单引号也可以用双引号 a='hello' a="hello" 如果字符串中存在单引号,可以用双引号里包含单引号的方式 a="I ...

  4. 模板—数学—Exgcd

    模板—数学—Exgcd Code: #include <cstdio> #include <algorithm> using namespace std; int ex_gcd ...

  5. intellij idea 为JavaEE项目建立Servlet

    建立Servlet的方法 顶部菜单栏 View > Tool Windows > Web. 然后互相web窗口 右键Web>new>Servlet 弹出窗口

  6. URAL 2072 Kirill the Gardener 3 (单调DP)

    [题目链接] http://acm.timus.ru/problem.aspx?space=1&num=2072 [题目大意] 一个园丁要给一排花浇水,每个花都有一个标号,必须要先浇标号小的, ...

  7. 【spfa】bzoj3921 Mimori与树海

    考虑“删除后图仍连通”,即其不是无向图的桥(bridge),可以用Tarjan算法预处理,这里不赘述. [算法一] 枚举删除的是哪条边,然后枚举起点,暴搜,统计答案. 可以通过0.1号测试点. 预计得 ...

  8. 1.6(学习笔记)Session

    一. Session简介 Session是用于解决HTTP无状态问题,HTTP协议本身是没有状态的, 就类似一个没有记性的商人,每次只交易当前的货物,交易完后就忘记了 以前的交易历史.我们和商人交易时 ...

  9. Java小问题的解决方法系列

    1)IDEA中文乱码,解决方法:http://blog.csdn.net/zht666/article/details/8953516 2)卸载OpenJdk,http://my.oschina.ne ...

  10. Java高级架构师(一)第03节:多模块多Web应用合并War包

    多模块.多Web应用合并war包 在日常的系统开发中,如果担心各个系统的资源同名覆盖,可以在总的War模块下放置一份最终的资源. 将版本号改成9.1.0.v20131115,ok 在Idea中的Mav ...