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. iOS 动画整理

    序列帧动画 曾经项目里的一段源码: 1234567891011121314 UIImageView * activityImageView = [[UIImageView alloc] init];N ...

  2. centos6.5 挂载远程目录

    查看nfs程序是否安装: [root@crawler_mv02 ~]# rpm -qa |grep rpcbindrpcbind-0.2.0-13.el6_9.1.x86_64[root@crawle ...

  3. 发布message给其他包使用

    https://answers.ros.org/question/65716/which-is-the-correct-way-to-install-header-files-in-catkin-pa ...

  4. 走进Spark--云计算大数据新一代技术

    什么是Spark? 当然这里说的Spark指的是Apache Spark, Apache Spark™ is a fast and general engine for large-scale dat ...

  5. django 实现自定义认证

    1.Django自带用户认证系统 Django自带用户认证系统,这个系统支持访问控制.注册用户.关联创建者和内容等:在开发用户认证功能时的时候,可以使用Django自带用户认证系统实现: A.相关表 ...

  6. css :not 选择器

    :not 选择器是css3里面的 :not([class]){color:red;}  // 没有class属性的元素都设置为红色 p:not([class]){color:red;} // 没有cl ...

  7. strcmp()函数-比较字符串的大小、字符串排序

    1.比较字符串的大小: 用法:strcmp(字符串1,字符串2),若字符串1>字符串2 则返回1,字符串1<字符串2 则返回 -1,相等返回0. 比较两个字符串的算法是:逐个比较两个串中对 ...

  8. 3、Flask实战第3天:url_for使用

    我们之前是通过url来找到对应的视图函数 /     =>    hello_world 那么url_for则是通过视图函数找到url hello world  =>  / 演示如下 .. ...

  9. [BZOJ 4057] Kingdoms

    Link: BZOJ 4057 传送门 Solution: 一道比较基础的状压DP 看到$n<=20$,状态转移与顺序相关,就可以开心地状压了 1表示破产,0表示未破产,不断扩展破产的集合即可 ...

  10. [xsy2962]作业

    题意:$f_0=1-\dfrac1e,f_n=1-nf_{n-1}$,求$f_n(n\leq10000)$,保留四位小数 这题代码只有⑨行但是题解很神... 因为递推式中有乘法,所以直接按题目来推肯定 ...