-
Notifications
You must be signed in to change notification settings - Fork 13
Description
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 Iteration
Skip
Skip the first n elements in the iterable.
public static function skip(iterable $data, int $n): \GeneratorShuffle
Shuffle the elements in the iterable.
public static function shuffle(iterable $data): \GeneratorNote: More useful in a Stream than standalone function.
Filter RegEx
Filter for elements where the regular expression matches.
public static function filterRegEx(iterable $data, string $regex): \GeneratorPermutations
All permutations of size $length.
public static function permutations(iterable $data, int $length) \GeneratorCombinations
All combinations of size $length.
public static function combinations(iterable $data, int $length) \GeneratorCombinations With Replacement
All combinations, with repeated elements, of size $length.
public static function combinationsWithReplacement(iterable $data, int $length) \GeneratorMulti
Unzip
Reverse of zip.
Ex: ['a', 1], ['b', 2], ['c', 3] => ['a', 'b', 'c'], [1, 2, 3]
public static function unzip(...iterable $iterables): arrayReduce
To Random Value
Reduce the iterable to any random value in the iterable.
public static function toRandomValue(iterable $data): mixedNote: More useful in a Stream than standalone function.
To Nth
Reduce the iterable to the value at the nth position.
public static function toNth(iterable $data): mixedNote: More useful in a Stream than standalone function.
To Frequencies
Reduce the iterable to a frequency distribution showing how often each different value in the data occurs.
public static function toFrequencies(iterable $data): arrayNote: MathPHP has this functionality. Maybe outside the scope for IterTools.
To Summary Stats
Reduce the iterable (of numbers) to a five-number summary.
public static function toSummaryStats(iterable $data): arrayNote: MathPHP has this functionality. Maybe outside the scope for IterTools.
Summary
Not All Match
True if not all the elements are true according to the predicate.
public static function notAllMatch(iterable $data, callable $predicate): boolIs Empty
True if the iterable is empty.
public static function isEmpty(iterable $data): boolNote: More useful in a Stream than standalone function.
All Unique
True if all elements of the iterable are unique values.
public static function allUnique(iterable $data): boolAll Equal
True if all elements of the iterable are equal.
public static funcion allEqual(iterable $data): boolNote: Consider maybe adding a strict parameter, or alternate method allSame.
Set
Union
Difference
CartesianProduct
Transform
Distribute
Distribute the elements of the iterable evenly into n smaller iterables.
Ex: [1, 2, 3, 4], 2 => [1, 3], [2, 4]
public static function distribute(iterable $data, int $n): arrayDivide
Divide the elements of the iterable evenly into n smaller iterables, maintaining order.
Ex: [1, 2, 3, 4], 2 => [1, 2], [3, 4]
public static function divide(iterable $data, int $n): arrayStream
Peek
Peek at each element in between other Stream operations to do some action without modifying the stream. Useful for debugging purposes.
public function peek(callable $action): StreamIt might be useful to also add pre-built peaks:
peakPrintpeekPrintR
Example usage:
Stream::of($someData)
->map($someFunction)
->filter($anotherFunction)
->peakPrint() // Added for debugging during development to see what is going on
->toAverage()FYI: @Smoren.