bpolaszek /
php-iterable-functions
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace BenTools\IterableFunctions; |
||
| 6 | |||
| 7 | use ArrayIterator; |
||
| 8 | use EmptyIterator; |
||
| 9 | use Traversable; |
||
| 10 | |||
| 11 | use function array_values; |
||
| 12 | use function iterator_to_array; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Maps a callable to an iterable. |
||
| 16 | * |
||
| 17 | * @param iterable<TKey, TValue> $iterable |
||
| 18 | * @param callable(TValue):TResult $mapper |
||
| 19 | * |
||
| 20 | * @return iterable<TKey, TResult> |
||
| 21 | * |
||
| 22 | * @template TKey |
||
| 23 | * @template TValue |
||
| 24 | * @template TResult |
||
| 25 | */ |
||
| 26 | function iterable_map(iterable $iterable, callable $mapper): iterable |
||
| 27 | { |
||
| 28 | return iterable($iterable)->map($mapper); |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Copy the iterable into an array. |
||
| 33 | * |
||
| 34 | * @param iterable<array-key, TValue> $iterable |
||
| 35 | * @param bool $preserveKeys [optional] Whether to use the iterator element keys as index. |
||
| 36 | * |
||
| 37 | * @return array<array-key, TValue> |
||
| 38 | * |
||
| 39 | * @psalm-return ($preserveKeys is true ? array<TKey, TValue> : array<int, TValue>) |
||
| 40 | * @template TKey of array-key |
||
| 41 | * @template TValue |
||
| 42 | */ |
||
| 43 | function iterable_to_array(iterable $iterable, bool $preserveKeys = true): array |
||
| 44 | { |
||
| 45 | if ($iterable instanceof Traversable) { |
||
| 46 | return iterator_to_array($iterable, $preserveKeys); |
||
| 47 | } |
||
| 48 | |||
| 49 | return $preserveKeys ? $iterable : array_values($iterable); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * If the iterable is not instance of Traversable, it is an array => convert it to an ArrayIterator. |
||
| 54 | * |
||
| 55 | * @param iterable<TKey, TValue> $iterable |
||
| 56 | * |
||
| 57 | * @return Traversable<TKey, TValue> |
||
| 58 | * |
||
| 59 | * @template TKey |
||
| 60 | * @template TValue |
||
| 61 | */ |
||
| 62 | function iterable_to_traversable(iterable $iterable): Traversable |
||
| 63 | { |
||
| 64 | if ($iterable instanceof Traversable) { |
||
| 65 | return $iterable; |
||
| 66 | } |
||
| 67 | |||
| 68 | return new ArrayIterator($iterable); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Filters an iterable. |
||
| 73 | * |
||
| 74 | * @param (callable(TValue):bool)|null $filter |
||
| 75 | * |
||
| 76 | * @psalm-param iterable<TKey, TValue> $iterable |
||
| 77 | * @psalm-return iterable<TKey, TValue> |
||
| 78 | * @template TKey |
||
| 79 | * @template TValue |
||
| 80 | */ |
||
| 81 | function iterable_filter(iterable $iterable, ?callable $filter = null): iterable |
||
| 82 | { |
||
| 83 | return iterable($iterable)->filter($filter); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Reduces an iterable. |
||
| 88 | * |
||
| 89 | * @param iterable<TValue> $iterable |
||
| 90 | * @param TResult $initial |
||
| 91 | * @param callable(TResult, TValue):TResult $reduce |
||
| 92 | * |
||
| 93 | * @return TResult |
||
| 94 | * |
||
| 95 | * @template TValue |
||
| 96 | * @template TResult |
||
| 97 | * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint |
||
| 98 | * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint |
||
| 99 | */ |
||
| 100 | function iterable_reduce(iterable $iterable, callable $reduce, $initial = null) |
||
| 101 | { |
||
| 102 | foreach ($iterable as $item) { |
||
| 103 | $initial = $reduce($initial, $item); |
||
| 104 | } |
||
| 105 | |||
| 106 | return $initial; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Yields iterable values (leaving out keys). |
||
| 111 | * |
||
| 112 | * @param iterable<TValue> $iterable |
||
| 113 | * |
||
| 114 | * @return iterable<int, TValue> |
||
| 115 | * |
||
| 116 | * @template TValue |
||
| 117 | */ |
||
| 118 | function iterable_values(iterable $iterable): iterable |
||
| 119 | { |
||
| 120 | return iterable($iterable)->values(); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param iterable<TKey, TValue>|null $iterable |
||
| 125 | * |
||
| 126 | * @return IterableObject<TKey, TValue> |
||
| 127 | * |
||
| 128 | * @template TKey |
||
| 129 | * @template TValue |
||
| 130 | */ |
||
| 131 | function iterable(?iterable $iterable): IterableObject |
||
| 132 | { |
||
| 133 | return new IterableObject($iterable ?? new EmptyIterator()); |
||
| 134 | } |
||
| 135 |