[Javascript] Convert a forEach method to generator
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的更多相关文章
- [Javascript] The Array forEach method
Most JavaScript developers are familiar with the for loop. One of the most common uses of the for lo ...
- javascript: Convert special characters to HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- [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 ...
- JavaScript Patterns 5.9 method() Method
Advantage Avoid re-created instance method to this inside of the constructor. method() implementatio ...
- JavaScript – Convert Image to Base64 String
From: https://bytenota.com/javascript-convert-image-to-base64-string/ his post shows you two approac ...
- 理解JavaScript里的 [].forEach.call() 写法
原文: http://www.webhek.com/javascript-foreach-call document.querySelectorAll() 返回的并不是我们想当然的数组,而是 Nod ...
- JavaScript中的forEach
语法:array.forEach(callbackfn[, thisArg]) 参数说明: array1 必需. 一个数组对象. callbackfn 必需. 一个接受最多三个参数的函数. 对 ...
- [Javascript] The Array filter method
One very common operation in programming is to iterate through an Array's contents, apply a test fun ...
- [Javascript] The Array map method
One very common operation in programming is to iterate through an Array's contents, apply a function ...
随机推荐
- @ConfigurationProperties和@Value的区别
@ConfigurationProperties @Value 功能: 批量注入配置文件中的属性 一个个指定,多个属性多个@Value 松散绑定: 支持 不支持 SpEL: 不支持 支持 JSR ...
- PAT甲级 模拟题_C++题解
模拟题 PAT (Advanced Level) Practice 模拟题 目录 1008 Elevator (20) 1042 Shuffling Machine (20) 1046 Shortes ...
- 使用内存地址点亮LED—跟51单片机一样写代码教学(初步入门)
51单片机点亮一个小灯 #include <rge52.h> sbit LED = P0^ void main(void) { P0 = 0XFE; // 总线操作 sfr P0 0X80 ...
- 【数据结构】P1981 表达式求值
题目描述 给定一个只包含加法和乘法的算术表达式,请你编程计算表达式的值. 输入格式 一行,为需要你计算的表达式,表达式中只包含数字.加法运算符“++”和乘法运算符“×”,且没有括号,所有参与运算的数字 ...
- JVM 介绍
JVM 介绍: JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的 ...
- C# 钉钉第三方开发接入
钉钉开放平台 本文是针对钉钉开放平台的基于dotNetCore服务端开发和配置的描述 钉钉可开发的程序包括 企业内部应用,第三方企业应用,第三方个人应用 一.环境搭建 1.钉钉开发需要企业钉钉账号,如 ...
- row_number() over()函数基本用法
简单的说row_number()从1开始,为每一条分组记录返回一个数字,这里的ROW_NUMBER() OVER (ORDER BY xlh DESC) 是先把xlh列降序,再为降序以后的没条xlh记 ...
- nlopt 二次优化
/* * main.c * * Created on: Oct 9, 2018 * Author: lgh */ #include <stdio.h> #include <math. ...
- ndk-build 修改输出so位置 (change ndk-build output so lib file path )
期望的目录结构: Folder --- | --- build.bat | --- Source | --- All sources codes *.cpp *.h | --- Android --- ...
- socket基本用法
socket介绍 1.什么是socket socket是应用层与传输层中间的一个软件抽象层,它是一组接口.它把TCP/IP这些复杂的协议统一封装起来 这样我们只要知道如何使用socket就好,就已经符 ...