-
Notifications
You must be signed in to change notification settings - Fork 602
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
tracking: support iterables in @std/collections
APIs
#5470
Comments
A larger change to consider. Should collections use generators? This would be breaking change. Generators give lazy evaluation, memory efficiency, and sometimes better performance Example: function* takeWhile<T>(
iterable: Iterable<T>,
predicate: (el: T) => boolean,
): Generator<T> {
for (const element of iterable) {
console.log(`takeWhile(): ${element}`);
if (!predicate(element)) {
break;
}
yield element;
}
}
function* map<T, U>(
iterable: Iterable<T>,
transform: (el: T) => U,
): Generator<U> {
for (const element of iterable) {
console.log(`map(): ${element}`);
yield transform(element);
}
}
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const result = takeWhile(map(arr, (n) => n * 100), n => n < 500)
console.log([...result]) |
I think we should stick to the current approach unless the performance benefits are sufficient. We'd have to measurements before coming to that decision. |
The move to iterables means that generators that never return are valid arguments. function* counter() {
let i = 0;
while (true) {
yield i++;
}
} Many of these functions would hang Examples: takeWhile(counter(), v === 0) // predicate will never be true
chunk(counter(), 10)
dropLastWhile(counter, v => v > 30)
dropWhile(counter, v => v === 0) // predicate will never be true Here are some options:
|
Following on from #4671, it seems there are APIs within
@std/collections
that could supportIterables
rather than just arrays. From a quick scan, these symbols might be able to be tweaked to support iterables:chunk()
dropLastWhile()
dropWhile()
intersect()
sample()
slidingWindows()
sortBy()
takeLastWhile()
takeWhile()
withoutAll()
See #3390 for an example of how with was done for
groupBy()
.The text was updated successfully, but these errors were encountered: