[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 ...
随机推荐
- Excel常用操作1
1.数据透视 所在选项卡:插入-数据透视表 例如:查看下表中各个工龄的平均工资 数据透视:选择所有数据--数据透视表--数据透视字段:选择工作经验和salary 切片器的使用,根据工作经验进行切片(还 ...
- Python习题005
作业一 :任意一个数字列表,然后进行排序(冒泡排序) 方法一: def test1(): list1 = [1,23,4,6,8,55,2,9,90,35] list1.sort() # sort() ...
- java——值传递和引用传递
值传递 在方法被调用时,实参通过形参把它的内容副本传入方法内部,此时形参接收到的内容是实参值的一个拷贝,因此在方法内对形参的任何操作,都仅仅是对这个副本的操作,不影响原始值的内容. 先来看个例子: p ...
- 使用jdk8 stream简化集合操作
使用stream的前提是对lambda表达式和函数式接口有一定的了解,同时对方法引用和普通传参的区别有一定的认识. stream的三大特性:1.不存储数据2.不改变源数据3.延时执行. stream优 ...
- (十七)Hibnernate 和 Spring 整合
一.Hibnernate 和 Spring结合方案: 方案一: 框架各自使用自己的配置文件,Spring中加载Hibernate的配置文件. 方案二: 统一由Spring的配置来管理.(推荐使用 ...
- (六)发送、接收SOAP消息(1)
一.为什么要用soap 原本我们使用web服务都是根据wsdl生成客户端(生成一堆java文件)然后再调用,本章节讲解如何用soap消息来替代这种方式. 二.SOAP消息格式 SOAP(简单对象访问协 ...
- java 框架-分布式文件管理系统1FastDFS
https://www.cnblogs.com/chiangchou/p/fastdfs.html
- Linux基本命令 vi操作和插件
基本命令 vim命令 常用插件 基本命令 查看Tomcat日志: tail -n 20 -f catalina.out 查看指定列表: find ./ -name 'ser*' 搜索指定文件: loc ...
- Linux--CentOS 安装 Docker 教程
本文主要介绍 CentOS 系统安装 Docker 的流程. 前提条件 OS 要求 CentOS7: The centos-extras repository must be enabled. Thi ...
- 表格中的DOM
通过DOM来操作table跟在html中操作table是不一样的,下面来看看怎样通过DOM来操作table. 按照table的分布来创建: <table> <thead> &l ...