itertools-ts
itertools-ts copied to clipboard
New IterTools Functionality Discussion
This thread is for discussion new functionality to implement in IterTools. Please reply with suggestions for new functionality.
Here are some of the functions I am considering implementing. Some are more useful than others.
Single
- [ ] #59
- [ ] #60
- [ ] #61
Combinatorics
- [ ] #62
Multi
- [ ] #63
Reduce
- [ ] #64
- [ ] #65
- [ ] #66
- [ ] #67
Summary
- [ ] #68
- [ ] #69
- [ ] #70
- [ ] #71
- [ ] #72
Set
- [ ] #73
Transform
- [ ] #74
- [ ] #75
Random
- [ ] #76
- [ ] #77
- [ ] #78
- [ ] #79
- [ ] #80
- [ ] #81
@Smoren First of all, thank you for a wonderful library.
I guess it's pretty hard to implement a FP version of it where functions are curried and parameters are inversed using TS, right?
An example:
const takeWhile = _.curry(function* takeWhile(predicateFn, iterable) {
for (const item of iterable) {
if (predicateFn(item)) {
yield item;
} else {
break;
}
}
});
const limitAsync = _.curry(async function* limitAsync(count, iterable) {
if (count < 0) {
throw new TypeError(`Limit must be ≥ 0. Got ${count}`);
}
let i = 0;
for await (const datum of iterable) {
if (i >= count) {
return;
}
yield datum;
++i;
}
});
// …
const asyncIterable = httpClient.fetchSomePaginatedDataIterable();
const fetch100ItemsOrLess = _.compose(
toArrayAsync,
limitAsync(100),
flatMapAsync(response => response.data),
);
const result = await fetch100ItemsOrLess(asyncIterable);
Hi @zveroboy! Thank you for your feedback! I think you can use streams for such tasks.
import { AsyncStream } from 'itertools-ts';
const fetch100ItemsOrLess = async (response) => await AsyncStream.of(response)
.flatMap(response => response.data)
.limit(100)
.toArray();
const result = await fetch100ItemsOrLess(asyncIterable);