Prepare repository for merge

This commit is contained in:
Angelos Chalaris
2023-05-01 22:35:56 +03:00
parent fc4e61e6fa
commit b3ad01863a
578 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,28 @@
---
title: Flat iterator
type: snippet
tags: [array,iterator,generator]
author: chalarangelo
cover: balloons
dateModified: 2022-03-09T05:00:00-04:00
---
Creates a generator that iterates over an iterable, flattening nested iterables.
- Use recursion.
- Use a `for...of` loop to iterate over the values of the given iterable.
- Use `Symbol.iterator` to check if each value is an iterable. If it is, use the `yield*` expression to recursively delegate to the same generator function. Otherwise, `yield` the current value.
```js
const flatIterator = function* (itr) {
for (let item of itr) {
if (item[Symbol.iterator]) yield* flatIterator(item);
else yield item;
}
};
```
```js
const arr = [1, 2, [3, 4], [5, [6, [7], 8]], 9, new Set([10, 11])];
[...flatIterator(arr)]; // 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
```