|
7 | 7 | use Fp\Functional\Either\Either; |
8 | 8 | use Fp\Functional\Option\Option; |
9 | 9 | use Fp\Operations\TraverseEitherAccOperation; |
| 10 | +use Fp\Operations\TraverseEitherMergeOperation; |
10 | 11 | use Fp\Operations\TraverseEitherOperation; |
11 | 12 | use Fp\Operations\TraverseOptionOperation; |
12 | 13 |
|
@@ -88,6 +89,78 @@ function traverseEither(iterable $collection, callable $callback): Either |
88 | 89 | return traverseEitherKV($collection, dropFirstArg($callback)); |
89 | 90 | } |
90 | 91 |
|
| 92 | +/** |
| 93 | + * Same as {@see traverseEither()}, but passing also the key to the $callback function. |
| 94 | + * |
| 95 | + * @template E |
| 96 | + * @template TK of array-key |
| 97 | + * @template TV |
| 98 | + * @template TVO |
| 99 | + * |
| 100 | + * @param iterable<TK, TV> $collection |
| 101 | + * @param callable(TK, TV): Either<E, TVO> $callback |
| 102 | + * @return Either<E, array<TK, TVO>> |
| 103 | + * |
| 104 | + * @psalm-return ( |
| 105 | + * $collection is non-empty-list ? Either<E, non-empty-list<TVO>> : |
| 106 | + * $collection is list ? Either<E, list<TVO>> : |
| 107 | + * $collection is non-empty-array ? Either<E, non-empty-array<TK, TVO>> : |
| 108 | + * Either<E, array<TK, TVO>> |
| 109 | + * ) |
| 110 | + */ |
| 111 | +function traverseEitherKV(iterable $collection, callable $callback): Either |
| 112 | +{ |
| 113 | + return TraverseEitherOperation::of($collection)($callback)->map(asArray(...)); |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Similar to {@see traverseEither} but collects all errors to non-empty-list. |
| 118 | + * |
| 119 | + * @template E |
| 120 | + * @template TK of array-key |
| 121 | + * @template TV |
| 122 | + * @template TVO |
| 123 | + * |
| 124 | + * @param iterable<TK, TV> $collection |
| 125 | + * @param callable(TV): Either<non-empty-list<E>, TVO> $callback |
| 126 | + * @return Either<non-empty-list<E>, array<TK, TVO>> |
| 127 | + * |
| 128 | + * @psalm-return ( |
| 129 | + * $collection is non-empty-list ? Either<non-empty-list<E>, non-empty-list<TVO>> : |
| 130 | + * $collection is list ? Either<non-empty-list<E>, list<TVO>> : |
| 131 | + * $collection is non-empty-array ? Either<non-empty-list<E>, non-empty-array<TK, TVO>> : |
| 132 | + * Either<non-empty-list<E>, array<TK, TVO>> |
| 133 | + * ) |
| 134 | + */ |
| 135 | +function traverseEitherMerge(iterable $collection, callable $callback): Either |
| 136 | +{ |
| 137 | + return traverseEitherKVMerge($collection, dropFirstArg($callback)); |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * Same as {@see traverseEitherMerge()}, but passing also the key to the $callback function. |
| 142 | + * |
| 143 | + * @template E |
| 144 | + * @template TK of array-key |
| 145 | + * @template TV |
| 146 | + * @template TVO |
| 147 | + * |
| 148 | + * @param iterable<TK, TV> $collection |
| 149 | + * @param callable(TK, TV): Either<non-empty-list<E>, TVO> $callback |
| 150 | + * @return Either<non-empty-list<E>, array<TK, TVO>> |
| 151 | + * |
| 152 | + * @psalm-return ( |
| 153 | + * $collection is non-empty-list ? Either<non-empty-list<E>, non-empty-list<TVO>> : |
| 154 | + * $collection is list ? Either<non-empty-list<E>, list<TVO>> : |
| 155 | + * $collection is non-empty-array ? Either<non-empty-list<E>, non-empty-array<TK, TVO>> : |
| 156 | + * Either<non-empty-list<E>, array<TK, TVO>> |
| 157 | + * ) |
| 158 | + */ |
| 159 | +function traverseEitherKVMerge(iterable $collection, callable $callback): Either |
| 160 | +{ |
| 161 | + return TraverseEitherMergeOperation::of($collection)($callback)->map(asArray(...)); |
| 162 | +} |
| 163 | + |
91 | 164 | /** |
92 | 165 | * Same as {@see traverseEither()} but accumulates all left errors. |
93 | 166 | * |
@@ -141,27 +214,3 @@ function traverseEitherKVAcc(iterable $collection, callable $callback): Either |
141 | 214 | }) |
142 | 215 | ->map(asArray(...)); |
143 | 216 | } |
144 | | - |
145 | | -/** |
146 | | - * Same as {@see traverseEither()}, but passing also the key to the $callback function. |
147 | | - * |
148 | | - * @template E |
149 | | - * @template TK of array-key |
150 | | - * @template TV |
151 | | - * @template TVO |
152 | | - * |
153 | | - * @param iterable<TK, TV> $collection |
154 | | - * @param callable(TK, TV): Either<E, TVO> $callback |
155 | | - * @return Either<E, array<TK, TVO>> |
156 | | - * |
157 | | - * @psalm-return ( |
158 | | - * $collection is non-empty-list ? Either<E, non-empty-list<TVO>> : |
159 | | - * $collection is list ? Either<E, list<TVO>> : |
160 | | - * $collection is non-empty-array ? Either<E, non-empty-array<TK, TVO>> : |
161 | | - * Either<E, array<TK, TVO>> |
162 | | - * ) |
163 | | - */ |
164 | | -function traverseEitherKV(iterable $collection, callable $callback): Either |
165 | | -{ |
166 | | - return TraverseEitherOperation::of($collection)($callback)->map(asArray(...)); |
167 | | -} |
|
0 commit comments