Open Smoren opened 7 months ago
@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);
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
Shuffle
Shuffle the elements in the iterable.
Filter RegEx
Filter for elements where the regular expression matches.
Permutations
All permutations of size
length
.Combinations
All combinations of size
length
.Combinations With Replacement
All combinations, with repeated elements, of size
length
.Multi
Unzip
Reverse of zip. E.g.: ['a', 1], ['b', 2], ['c', 3] => ['a', 'b', 'c'], [1, 2, 3]
Note: specify types.
Reduce
To Random Value
Reduce the iterable to any random value in the iterable.
PHP implementation example
To Nth
Reduce the iterable to the value at the nth position.
PHP implementation example
To Frequencies
Reduce the iterable to a frequency distribution showing how often each different value in the data occurs.
To String
Reduces to a string joining all elements.
PHP implementation example
Summary
Not All Match
True if not all the elements are true according to the predicate.
Is Empty
True if the iterable is empty.
PHP implementation example
All Equal
True if all elements of the iterable are equal.
Note: Consider maybe adding a strict parameter, or alternate method
allSame
.Are Permutations
True if iterables are permutations of each other.
PHP implementation example
Is Partitioned
Returns true if all elements of given collection that satisfy the predicate appear before all elements that don't.
PHP implementation example
Set
Difference
Compares first iterable against one or more other iterables and iterates the values in array that are not present in any of the other arrays.
Transform
Distribute
Distribute the elements of the iterable evenly into n smaller iterables. Ex: ([1, 2, 3, 4], 2) => [1, 3], [2, 4]
Divide
Divide the elements of the iterable evenly into n smaller iterables, maintaining order. Ex: ([1, 2, 3, 4], 2) => [1, 2], [3, 4]
Random
Choice
Generate random selections from an array of values.
If optional param
repetitions
not given then iterate infinitely.PHP implementation example
Coin Flip
Generate random coin flips (0 or 1).
If optional param
repetitions
not given then iterate infinitely.PHP implementation example
Bool
Generate random boolean values.
If optional param
repetitions
not given then iterate infinitely.Number
Generate random integer numbers.
If optional param
repetitions
not given then iterate infinitely.PHP implementation example
Percentage
Generate a random percentage between 0 and 1.
If optional param
repetitions
not given then iterate infinitely.PHP implementation example
RockPaperScissors
Generate random rock-paper-scissors hands. Return values: "rock", "paper", "scissors".
If optional param
repetitions
not given then iterate infinitely.PHP implementation example