Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: iterator chain / concatenation #771

Open
keturn opened this issue Oct 27, 2024 · 1 comment
Open

feature: iterator chain / concatenation #771

keturn opened this issue Oct 27, 2024 · 1 comment

Comments

@keturn
Copy link

keturn commented Oct 27, 2024

Python calls it itertools.chain:

chain("ABC", [1, 2, 3]).toArray() === ["A", "B", "C", 1, 2, 3]

Like Array.concat, but lazy, and doesn't need its iterables to be marked with Symbol.isConcatSpreadable.

(does the toolkit not have an Iterator Utilities section yet?)

@keturn
Copy link
Author

keturn commented Oct 27, 2024

I believe this may be implemented as

function chain(...iterators) {
    return iterators.flatMap(identity);
}

which is succinct, but a bit quirky maybe.

Alternatively,

function* chain(...iterators) {
    for (const iterator of iterators) {
        yield* iterator;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant