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. 关于db2中listagg函数开发中的体验

    一.首先解释一下可能会查询的基础问题: 1.1db2 “with ur”是什么意思: 在DB2中,共有四种隔离级:RS,RR,CS,UR.以下对四种隔离级进行一些描述,同时附上个人做试验的结果.隔离级 ...

  2. C++实现16进制字符串转换成int整形值

    开发中经常需要把16进制字符串转换成整形,写了个个代码供大家参考下: #include <stdio.h> #include <string.h> //字符转换成整形 int ...

  3. LC 144. / 94. / 145. Binary Tree Preorder/ Inorder/ PostOrder Traversal

    题目描述 144. Binary Tree Preorder Traversal 94. Binary Tree Inorder Traversal 145. Binary Tree Postorde ...

  4. js — 字符串

    目录 1. 拼接字符串 2. 获取字符的方法 3. 字符串操作方法(切片) 4. 字符串位置方法 - 索引 5. trim()方法 6. 字符串大小写转换方法 字符串 typeof 用于校验当前变量的 ...

  5. 中国5级行政区域MySQL数据库库

    爬取国家统计局官网的行政区域数据,包括省市县镇村5个层级,其中港澳地区的数据只有3级;台湾地区4级;包含大陆地区的邮政编码和经纬度信息. 行政划分的总体结构是: 省->市(州)->县(区) ...

  6. opencv-01--图像的遍历

    遍历图像的4种方式 一.at<typename>(i,j) Mat类提供了一个at的方法用于取得图像上的点,它是一个模板函数,可以取到任何类型的图像上的点.下面我们通过一个图像处理中的实际 ...

  7. 函数实现计算等差数列的第n项

    等差数列的第n项 描述 等差数列是指从第二项起,每一项与它的前一项的差等于同一个常数的一种数列,这个常数叫做等差数列的公差.‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬ ...

  8. nodejs入门API之fs模块

    fs模块下的类与FS常量 fs模块下的主要方法 fs的Promise API与FileHandle类 一.fs模块下的类 1.1 fs.Dir:表示目录流的类,由 fs.opendir().fs.op ...

  9. FlowPortal BPM多汇报线的设置及使用

    1.在组织结构中设置多汇报线 2.流程中使用汇报线 3.流程节点上使用汇报线 流程节点默认启用流程中指定的汇报线,若流程中的某个节点需要启用特殊的汇报线,可通过设置节点业务属性实现.

  10. redis数据结构分析 (redisObject、SDS)

    redis是一个key-value储存系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...