1 | <?php |
||
2 | declare(strict_types = 1); |
||
3 | |||
4 | namespace Innmind\Immutable; |
||
5 | |||
6 | /** |
||
7 | * @template T |
||
8 | * @template S |
||
9 | * @psalm-immutable |
||
10 | */ |
||
11 | final class Map implements \Countable |
||
12 | { |
||
13 | private Map\Implementation $implementation; |
||
14 | |||
15 | private function __construct(Map\Implementation $implementation) |
||
16 | { |
||
17 | $this->implementation = $implementation; |
||
18 | } |
||
19 | |||
20 | /** |
||
21 | * Set a new key/value pair |
||
22 | * |
||
23 | * Example: |
||
24 | * <code> |
||
25 | 61 | * Map::of() |
|
26 | * (1, 2) |
||
27 | 61 | * (3, 4) |
|
28 | 61 | * </code> |
|
29 | 61 | * |
|
30 | 61 | * @param T $key |
|
31 | 61 | * @param S $value |
|
32 | 61 | * |
|
33 | * @return self<T, S> |
||
34 | 61 | */ |
|
35 | 4 | public function __invoke($key, $value): self |
|
36 | 60 | { |
|
37 | 50 | return new self(($this->implementation)($key, $value)); |
|
38 | } |
||
39 | 61 | ||
40 | /** |
||
41 | * @template U |
||
42 | * @template V |
||
43 | * @psalm-pure |
||
44 | * |
||
45 | * @param list<array{0: U, 1: V}> $pairs |
||
46 | * |
||
47 | 62 | * @return self<U, V> |
|
48 | */ |
||
49 | public static function of(array ...$pairs): self |
||
50 | { |
||
51 | $self = new self(new Map\Uninitialized); |
||
52 | |||
53 | 62 | foreach ($pairs as [$key, $value]) { |
|
54 | 62 | $self = ($self)($key, $value); |
|
55 | } |
||
56 | 62 | ||
57 | 1 | return $self; |
|
58 | } |
||
59 | |||
60 | 61 | public function size(): int |
|
61 | { |
||
62 | 61 | return $this->implementation->size(); |
|
63 | 1 | } |
|
64 | |||
65 | public function count(): int |
||
66 | 61 | { |
|
67 | return $this->size(); |
||
68 | } |
||
69 | 18 | ||
70 | /** |
||
71 | 18 | * Set a new key/value pair |
|
72 | * |
||
73 | * @param T $key |
||
74 | * @param S $value |
||
75 | * |
||
76 | * @return self<T, S> |
||
77 | 26 | */ |
|
78 | public function put($key, $value): self |
||
79 | 26 | { |
|
80 | return ($this)($key, $value); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Return the element with the given key |
||
85 | 24 | * |
|
86 | * @param T $key |
||
87 | 24 | * |
|
88 | * @return Maybe<S> |
||
89 | */ |
||
90 | 8 | public function get($key): Maybe |
|
91 | { |
||
92 | 8 | return $this->implementation->get($key); |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * Check if there is an element for the given key |
||
97 | * |
||
98 | 21 | * @param T $key |
|
99 | */ |
||
100 | 21 | public function contains($key): bool |
|
101 | { |
||
102 | return $this->implementation->contains($key); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Return an empty map of the same type |
||
107 | * |
||
108 | * @return self<T, S> |
||
109 | */ |
||
110 | public function clear(): self |
||
111 | 54 | { |
|
112 | return new self($this->implementation->clear()); |
||
113 | 54 | } |
|
114 | 53 | ||
115 | /** |
||
116 | 52 | * Check if the two maps are equal |
|
117 | 52 | * |
|
118 | * @param self<T, S> $map |
||
119 | 52 | */ |
|
120 | public function equals(self $map): bool |
||
121 | { |
||
122 | return $this->implementation->equals($map->implementation); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Filter the map based on the given predicate |
||
127 | * |
||
128 | * @param callable(T, S): bool $predicate |
||
129 | * |
||
130 | * @return self<T, S> |
||
131 | */ |
||
132 | public function filter(callable $predicate): self |
||
133 | { |
||
134 | return new self($this->implementation->filter($predicate)); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | 36 | * Run the given function for each element of the map |
|
139 | * |
||
140 | 36 | * @param callable(T, S): void $function |
|
141 | */ |
||
142 | public function foreach(callable $function): SideEffect |
||
143 | { |
||
144 | return $this->implementation->foreach($function); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Return a new map of pairs' sequences grouped by keys determined with the given |
||
149 | * discriminator function |
||
150 | * |
||
151 | * @template D |
||
152 | 37 | * |
|
153 | * @param callable(T, S): D $discriminator |
||
154 | 37 | * |
|
155 | * @return self<D, self<T, S>> |
||
156 | */ |
||
157 | 37 | public function groupBy(callable $discriminator): self |
|
158 | { |
||
159 | return $this->implementation->groupBy($discriminator); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Return all keys |
||
164 | * |
||
165 | 12 | * @return Set<T> |
|
166 | */ |
||
167 | 12 | public function keys(): Set |
|
168 | { |
||
169 | 12 | return $this->implementation->keys(); |
|
170 | } |
||
171 | |||
172 | /** |
||
173 | * Return all values |
||
174 | * |
||
175 | * @return Sequence<S> |
||
176 | */ |
||
177 | 5 | public function values(): Sequence |
|
178 | { |
||
179 | 5 | return $this->implementation->values(); |
|
180 | 5 | } |
|
181 | |||
182 | 5 | /** |
|
183 | * Apply the given function on all elements and return a new map |
||
184 | * |
||
185 | * @template B |
||
186 | * |
||
187 | * @param callable(T, S): B $function |
||
188 | * |
||
189 | * @return self<T, B> |
||
190 | 7 | */ |
|
191 | public function map(callable $function): self |
||
192 | 7 | { |
|
193 | return new self($this->implementation->map($function)); |
||
194 | 6 | } |
|
195 | |||
196 | /** |
||
197 | * Merge all Maps created by each value from the initial Map |
||
198 | * |
||
199 | * @template A |
||
200 | * @template B |
||
201 | * |
||
202 | * @param callable(T, S): self<A, B> $map |
||
203 | * |
||
204 | 1 | * @return self<A, B> |
|
205 | */ |
||
206 | 1 | public function flatMap(callable $map): self |
|
207 | 1 | { |
|
208 | /** |
||
209 | 1 | * @psalm-suppress InvalidArgument |
|
210 | * @psalm-suppress MixedArgument |
||
211 | */ |
||
212 | return $this->reduce( |
||
213 | self::of(), |
||
214 | static fn(self $carry, $key, $value) => $carry->merge($map($key, $value)), |
||
215 | ); |
||
216 | } |
||
217 | 1 | ||
218 | /** |
||
219 | 1 | * Remove the element with the given key |
|
220 | 1 | * |
|
221 | * @param T $key |
||
222 | * |
||
223 | * @return self<T, S> |
||
224 | */ |
||
225 | public function remove($key): self |
||
226 | { |
||
227 | return new self($this->implementation->remove($key)); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Create a new map by combining both maps |
||
232 | * |
||
233 | 2 | * @param self<T, S> $map |
|
234 | * |
||
235 | 2 | * @return self<T, S> |
|
236 | */ |
||
237 | public function merge(self $map): self |
||
238 | { |
||
239 | return new self($this->implementation->merge($map->implementation)); |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | 10 | * Return a map of 2 maps partitioned according to the given predicate |
|
244 | * |
||
245 | 10 | * @param callable(T, S): bool $predicate |
|
246 | * |
||
247 | * @return self<bool, self<T, S>> |
||
248 | */ |
||
249 | public function partition(callable $predicate): self |
||
250 | { |
||
251 | return $this->implementation->partition($predicate); |
||
252 | } |
||
253 | 9 | ||
254 | /** |
||
255 | 9 | * Reduce the map to a single value |
|
256 | * |
||
257 | * @template R |
||
258 | * @param R $carry |
||
259 | * @param callable(R, T, S): R $reducer |
||
260 | * |
||
261 | * @return R |
||
262 | */ |
||
263 | public function reduce($carry, callable $reducer) |
||
264 | { |
||
265 | return $this->implementation->reduce($carry, $reducer); |
||
266 | } |
||
267 | 3 | ||
268 | public function empty(): bool |
||
269 | 3 | { |
|
270 | 3 | return $this->implementation->empty(); |
|
271 | } |
||
272 | 1 | ||
273 | /** |
||
274 | * @param callable(T, S): bool $predicate |
||
275 | * |
||
276 | * @return Maybe<Pair<T, S>> |
||
277 | */ |
||
278 | public function find(callable $predicate): Maybe |
||
279 | { |
||
280 | return $this->implementation->find($predicate); |
||
281 | } |
||
282 | 1 | ||
283 | /** |
||
284 | 1 | * @param callable(T, S): bool $predicate |
|
285 | */ |
||
286 | 1 | public function matches(callable $predicate): bool |
|
287 | 1 | { |
|
288 | /** @psalm-suppress MixedArgument */ |
||
289 | 1 | return $this->reduce( |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
290 | true, |
||
291 | static fn(bool $matches, $key, $value): bool => $matches && $predicate($key, $value), |
||
292 | ); |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * @param callable(T, S): bool $predicate |
||
297 | */ |
||
298 | public function any(callable $predicate): bool |
||
299 | 2 | { |
|
300 | return $this->find($predicate)->match( |
||
0 ignored issues
–
show
|
|||
301 | 2 | static fn() => true, |
|
302 | static fn() => false, |
||
303 | 1 | ); |
|
304 | 1 | } |
|
305 | } |
||
306 |