For example we have a 'forEach' method which can loop though a linked list:

forEach(fn) {
let node = this.head;
let counter = ;
while (node) {
fn(node, counter);
node = node.next;
counter++;
}
}

Test:

test('applies a transform to each node', () => {
const l = new List(); l.insertLast();
l.insertLast();
l.insertLast();
l.insertLast(); l.forEach(node => {
node.data += ;
}); expect(l.getAt().data).toEqual();
expect(l.getAt().data).toEqual();
expect(l.getAt().data).toEqual();
expect(l.getAt().data).toEqual();
});

What if we want to use for...of loop? Then we need to convert LinkedList to support generator partten, the usage of for..of:

test('works with the linked list', () => {
const l = new List(); l.insertLast();
l.insertLast();
l.insertLast();
l.insertLast(); for (let node of l) {
node.data += ;
} expect(l.getAt().data).toEqual();
expect(l.getAt().data).toEqual();
expect(l.getAt().data).toEqual();
expect(l.getAt().data).toEqual();
});

Implementation:

*[Symbol.iterator]() {
let node = this.head;
while (node) {
yield node;
node = node.next;
}
}

[Javascript] Convert a forEach method to generator的更多相关文章

  1. [Javascript] The Array forEach method

    Most JavaScript developers are familiar with the for loop. One of the most common uses of the for lo ...

  2. javascript: Convert special characters to HTML

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. [Javascript] Convert a Callback-Based JavaScript Function to a Promise-Based One

    Sometimes, you might want to convert a JavaScript function that accepts a callback to one that retur ...

  4. JavaScript Patterns 5.9 method() Method

    Advantage Avoid re-created instance method to this inside of the constructor. method() implementatio ...

  5. JavaScript – Convert Image to Base64 String

    From: https://bytenota.com/javascript-convert-image-to-base64-string/ his post shows you two approac ...

  6. 理解JavaScript里的 [].forEach.call() 写法

    原文:  http://www.webhek.com/javascript-foreach-call document.querySelectorAll() 返回的并不是我们想当然的数组,而是 Nod ...

  7. JavaScript中的forEach

    语法:array.forEach(callbackfn[, thisArg]) 参数说明: array1   必需. 一个数组对象. callbackfn   必需. 一个接受最多三个参数的函数. 对 ...

  8. [Javascript] The Array filter method

    One very common operation in programming is to iterate through an Array's contents, apply a test fun ...

  9. [Javascript] The Array map method

    One very common operation in programming is to iterate through an Array's contents, apply a function ...

随机推荐

  1. 开始使用 git(配置+常用命令)

    ▶ 注意 页面显示问题: -- 是两个短横线 - 是一个横短线 由于显示问题导致两个短横线之间没有空格,看起来像是一条横线,实则是两条短横线 ▶ git 常用命令 ◆ git add ● git ad ...

  2. Spring Boot系列教程十三:Spring boot集成Sentinel Redis

    前言 上一篇文章介绍了spring boot集成单点的redis,然而实际生产环境使用单点的redis风险很高,一旦宕机整个服务将无法使用,这篇文章介绍如何使用基于sentinel的redis高可用方 ...

  3. jquery 广告轮播图

    轮播图 /*轮播图基本功能: * 1图片切换 * 1.1图片在中间显示 * 1.2图片淡入淡出 * 2左右各有一个按钮 * 2.1点击左按钮,图片切换上一张 * 2.2点击右按钮,图片切换下一张 * ...

  4. PAT(B) 1021 个位数统计(Java)

    题目链接:1021 个位数统计 (15 point(s)) 代码 /** * Score 15 * Run Time 93ms * @author wowpH * @version 1.0 */ im ...

  5. 通过json_set函数,来修改data字段的值

    SELECT REPLACE(json_extract(param,'$.payFundAcc'),'"','') from t_external_trade_event where sta ...

  6. 阿里云kubernetes集群被xmrig挖矿程序入侵

    原因是由于Kubernetes Apiserver不安全配置所致,Apiserver提供了资源操作的唯一入口,并提供认证.授权.访问控制.API注册和发现等机制,所以apiserver的安全至关重要. ...

  7. 解决GitHub下载慢问题,不用修改HOSTS文件

    写这篇文章缘由是我用的一款Github上的软件软件版本更新,想去Github上下载新的版本,结果下载速度居然只有几k,还老是下载失败,然后去修改HOSTS文件(我看文章基本都是叫修改这个),修改完成后 ...

  8. (九) spring 使用自定义限定符注解

    案例一 定义接口  CD.java package interfacepackage; public interface CD { void play(); } 定义接口 player .java p ...

  9. 安装Docker step by step

    1. 系统要求 centos7以上   使用cat /etc/redhat-release查看系统版本,我的Centos 7.6 centos-extra 仓库 enable,默认是打开的 2.安装d ...

  10. 在CentOS部署AspNetCore网站

    前段时间某云服务器大促,就买了一台打算折腾一下,买了几个月,却啥也没做,就改了个初始密码.最近快到双十一了,另一家厂商相同配置的服务器价格又便宜了一大截,看来又得剁手了.从今年开始,搜索一下云服务器, ...