[Typescript] Simplify iteration of custom data structures in TypeScript with iterators (backwards iteration with for ... of.. loop)

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.

 

复制代码
interface Action {
  type: string;
}

interface ListNode<T> {
  value: T;
  next: ListNode<T>;
  prev: ListNode<T>;
}

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};
  }
}

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: actionNode1,
  next: null,
  value: action2
};
actionNode1.next = actionNode2;

let actionNode3: ListNode<Action> = {
  prev: actionNode2,
  next: null,
  value: action3
};
actionNode2.next = actionNode3;

let actionNode4: ListNode<Action> = {
  prev: actionNode3,
  next: null,
  value: action4
};
actionNode3.next = actionNode4;

const backwardsActionsList = new BackwardsActionIterator(
  actionNode4
);

for(let action of backwardsActionsList) {
  console.log(action.type);
}
复制代码

 

posted @   Zhentiw  阅读(189)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2019-09-27 [Javascript] Construct a Regex to Match Twitter Mentions with Regexr
2018-09-27 [RxJS] Implement RxJS `switchMap` by Canceling Inner Subscriptions as Values are Passed Through
2018-09-27 [RxJS] Implement RxJS `mergeMap` through inner Observables to Subscribe and Pass Values Through
2018-09-27 [RxJS] Chain RxJS Operators Together with a Custom `pipe` Function using Array.reduce
2018-09-27 [RxJS] Implement the `map` Operator from Scratch in RxJS
2017-09-27 [Python] Different ways to test multiple flags at once in Python
2017-09-27 [Angular] Use :host-context and the ::ng-deep selector to apply context-based styling
点击右上角即可分享
微信分享提示