Smoren /
sequence-php
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Smoren\Sequence\Functions; |
||||
| 4 | |||||
| 5 | use ArrayIterator; |
||||
| 6 | use IteratorIterator; |
||||
| 7 | use MultipleIterator; |
||||
| 8 | use Smoren\Sequence\Structs\IndexedArray; |
||||
| 9 | use Smoren\Sequence\Structs\Range; |
||||
| 10 | |||||
| 11 | /** |
||||
| 12 | * Creates iterable range. |
||||
| 13 | * |
||||
| 14 | * @param int $start start value |
||||
| 15 | * @param int<0, max>|null $size size of elements |
||||
| 16 | * @param int $step range step |
||||
| 17 | * |
||||
| 18 | * @return Range<int> iterable range |
||||
| 19 | */ |
||||
| 20 | function xrange(int $start, ?int $size = null, int $step = 1): Range |
||||
| 21 | { |
||||
| 22 | 12 | if ($size === null) { |
|||
| 23 | 5 | [$start, $size] = [0, $start]; |
|||
| 24 | } |
||||
| 25 | |||||
| 26 | 12 | return new Range($start, $size, $step); |
|||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
$start of type integer is incompatible with the type Smoren\Sequence\Structs\T expected by parameter $start of Smoren\Sequence\Structs\Range::__construct().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 27 | } |
||||
| 28 | |||||
| 29 | /** |
||||
| 30 | * Maps iterable collections and returns IndexedArray of mapped values. |
||||
| 31 | * |
||||
| 32 | * @template TInput |
||||
| 33 | * @template TOutput |
||||
| 34 | * |
||||
| 35 | * @param callable(TInput $item): TOutput $mapper |
||||
| 36 | * @param iterable<TInput> $collections |
||||
| 37 | * |
||||
| 38 | * @return IndexedArray<TOutput> |
||||
| 39 | */ |
||||
| 40 | function map(callable $mapper, iterable ...$collections): IndexedArray |
||||
| 41 | { |
||||
| 42 | 9 | $result = new IndexedArray(); |
|||
| 43 | |||||
| 44 | 9 | if (count($collections) === 0) { |
|||
| 45 | 1 | return $result; |
|||
| 46 | } |
||||
| 47 | |||||
| 48 | 8 | $it = new MultipleIterator(MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_NUMERIC); |
|||
| 49 | |||||
| 50 | 8 | foreach ($collections as $collection) { |
|||
| 51 | 8 | if (is_array($collection)) { |
|||
| 52 | 8 | $collection = new ArrayIterator($collection); |
|||
| 53 | } |
||||
| 54 | |||||
| 55 | 8 | $it->attachIterator(new IteratorIterator($collection)); |
|||
| 56 | } |
||||
| 57 | |||||
| 58 | 8 | foreach ($it as $values) { |
|||
| 59 | 7 | $result[] = $mapper(...$values); |
|||
| 60 | } |
||||
| 61 | |||||
| 62 | 8 | return $result; |
|||
| 63 | } |
||||
| 64 | |||||
| 65 | /** |
||||
| 66 | * Filters iterable collection and returns IndexedArray of filtered items. |
||||
| 67 | * |
||||
| 68 | * @template T |
||||
| 69 | * |
||||
| 70 | * @param iterable<T> $collection |
||||
| 71 | * @param callable(T $item): bool $filter |
||||
| 72 | * |
||||
| 73 | * @return IndexedArray<T> |
||||
| 74 | */ |
||||
| 75 | function filter(iterable $collection, callable $filter): IndexedArray |
||||
| 76 | { |
||||
| 77 | 6 | $result = new IndexedArray(); |
|||
| 78 | |||||
| 79 | 6 | foreach ($collection as $item) { |
|||
| 80 | 5 | if ($filter($item)) { |
|||
| 81 | 4 | $result[] = $item; |
|||
| 82 | } |
||||
| 83 | } |
||||
| 84 | |||||
| 85 | 6 | return $result; |
|||
| 86 | } |
||||
| 87 | |||||
| 88 | /** |
||||
| 89 | * Reduces iterable collection. |
||||
| 90 | * |
||||
| 91 | * @template TInput |
||||
| 92 | * @template TOutput |
||||
| 93 | * |
||||
| 94 | * @param iterable<TInput> $collection |
||||
| 95 | * @param callable(TOutput $carry, TInput $item): TOutput $reducer |
||||
| 96 | * @param TOutput $initialValue |
||||
|
0 ignored issues
–
show
The type
Smoren\Sequence\Functions\TOutput was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 97 | * |
||||
| 98 | * @return TOutput |
||||
| 99 | */ |
||||
| 100 | function reduce(iterable $collection, callable $reducer, $initialValue = null) |
||||
| 101 | { |
||||
| 102 | 9 | $carry = $initialValue; |
|||
| 103 | |||||
| 104 | 9 | foreach ($collection as $item) { |
|||
| 105 | 7 | $carry = $reducer($carry, $item); |
|||
| 106 | } |
||||
| 107 | |||||
| 108 | 9 | return $carry; |
|||
| 109 | } |
||||
| 110 |