[TypeScript] Custom data structures in TypeScript with iterators
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的更多相关文章
- A library of generic data structures
A library of generic data structures including a list, array, hashtable, deque etc.. https://github. ...
- 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 ...
- 剪短的python数据结构和算法的书《Data Structures and Algorithms Using Python》
按书上练习完,就可以知道日常的用处啦 #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving wit ...
- Persistent Data Structures
原文链接:http://www.codeproject.com/Articles/9680/Persistent-Data-Structures Introduction When you hear ...
- Go Data Structures: Interfaces
refer:http://research.swtch.com/interfaces Go Data Structures: Interfaces Posted on Tuesday, Decembe ...
- Choose Concurrency-Friendly Data Structures
What is a high-performance data structure? To answer that question, we're used to applying normal co ...
- 无锁数据结构(Lock-Free Data Structures)
一个星期前,我写了关于SQL Server里闩锁(Latches)和自旋锁(Spinlocks)的文章.2个同步原语(synchronization primitives)是用来保护SQL Serve ...
- [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 ...
- Manipulating Data Structures
Computer Science An Overview _J. Glenn Brookshear _11th Edition We have seen that the way data struc ...
随机推荐
- CSS边框属性
边框 圆角 border-radius border-top-left-radius border-top-right-radius border-bottom-left-radlius border ...
- centos7安装tengine强制使用HTTPS访问
操作系统:centos7.2 x64tengine:Tengine/2.2.0主机IP: 10.0.0.12 一.安装tengine 1.1 下载源码安装包 1.1.1 源码包pcre-8.40 ...
- OpenCV——Mat、CvMat、IplImage类型浅析【转】
OpenCV中常见的与图像操作有关的数据容器有Mat,cvMat和IplImage. 一.Mat类型:矩阵类型,Matrix. 在openCV中,Mat是一个多维的密集数据数组.可以用来处理向量和矩阵 ...
- Selenium2+python自动化67-用例失败自动截图【转载】
前言: 装饰器其实就是一个以函数作为参数并返回一个替换函数的可执行函数 上一篇讲到用装饰器解决异常后自动截图,不过并没有与unittest结合,这篇把截图的装饰器改良了下,可以实现用例执行失败自动截图 ...
- Selenium2+python自动化20-Excel数据参数化【转载】
前言 问: Python 获取到Excel一列值后怎么用selenium录制的脚本中参数化,比如对登录用户名和密码如何做参数化? 答:可以使用xlrd读取Excel的内容进行参数化.当然为了便于各位小 ...
- centos7当中的systemd及systemctl(节选)
全面进入centos7时代,这个东东是需要系统了解的. http://blog.jobbole.com/85070/?utm_source=blog.jobbole.com&utm_mediu ...
- 新电脑配置 git 同步github账户
1.下载安装git 2.初始化 仓库文件夹 git init 3.生成公钥ssh-keygen -t rsa -C "youremail@example.com"4.github ...
- owasp zap 安全审计工具 功能详解
一.persist session 该功能主要保存扫描分析的结果,方便下次继续分析 二.扫描策略 1.修改策略 A.入口 B.具体设置页面 C.设置完成后,发起主动扫描,在弹出的窗口可以选择策略 D. ...
- Codeforces Round 536 (Div. 2) (E)
layout: post title: Codeforces Round 536 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- Spoj Query on a tree III
题目描述 给出N个点的一棵树(N-1条边),节点有白有黑,初始全为白 有两种操作: 0 i : 改变某点的颜色(原来是黑的变白,原来是白的变黑) 1 v : 询问1到v的路径上的第一个黑点,若无,输出 ...