Innmind /
Immutable
| 1 | <?php |
||
| 2 | declare(strict_types = 1); |
||
| 3 | |||
| 4 | namespace Innmind\Immutable; |
||
| 5 | |||
| 6 | use Innmind\Immutable\Exception\CannotGroupEmptyStructure; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * @template T |
||
| 10 | */ |
||
| 11 | final class Set implements \Countable |
||
| 12 | { |
||
| 13 | private string $type; |
||
| 14 | private ValidateArgument $validate; |
||
| 15 | private Set\Implementation $implementation; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * {@inheritdoc} |
||
| 19 | */ |
||
| 20 | 78 | private function __construct(string $type, Set\Implementation $implementation) |
|
| 21 | { |
||
| 22 | 78 | $this->type = $type; |
|
| 23 | 78 | $this->validate = Type::of($type); |
|
| 24 | 78 | $this->implementation = $implementation; |
|
| 25 | 78 | } |
|
| 26 | |||
| 27 | /** |
||
| 28 | * @param T $values |
||
| 29 | * |
||
| 30 | * @return self<T> |
||
| 31 | */ |
||
| 32 | 66 | public static function of(string $type, ...$values): self |
|
| 33 | { |
||
| 34 | 66 | return new self($type, new Set\Primitive($type, ...$values)); |
|
| 35 | } |
||
| 36 | 66 | ||
| 37 | /** |
||
| 38 | * It will load the values inside the generator only upon the first use |
||
| 39 | * of the set |
||
| 40 | * |
||
| 41 | * Use this mode when the amount of data may not fit in memory |
||
| 42 | * |
||
| 43 | * @param \Generator<T> $generator |
||
| 44 | 3 | * |
|
| 45 | * @return self<T> |
||
| 46 | 3 | */ |
|
| 47 | public static function defer(string $type, \Generator $generator): self |
||
| 48 | 3 | { |
|
| 49 | return new self($type, new Set\Defer($type, $generator)); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * It will call the given function every time a new operation is done on the |
||
| 54 | * set. This means the returned structure may not be truly immutable as |
||
| 55 | * between the calls the underlying source may change. |
||
| 56 | 2 | * |
|
| 57 | * Use this mode when calling to an external source (meaning IO bound) such |
||
| 58 | * as parsing a file or calling an API |
||
| 59 | 2 | * |
|
| 60 | * @param callable(): \Generator<T> $generator |
||
| 61 | 2 | * |
|
| 62 | * @return self<T> |
||
| 63 | */ |
||
| 64 | public static function lazy(string $type, callable $generator): self |
||
| 65 | { |
||
| 66 | return new self($type, new Set\Lazy($type, $generator)); |
||
| 67 | 6 | } |
|
| 68 | |||
| 69 | /** |
||
| 70 | 6 | * @param mixed $values |
|
| 71 | * |
||
| 72 | 6 | * @return self<mixed> |
|
| 73 | */ |
||
| 74 | public static function mixed(...$values): self |
||
| 75 | { |
||
| 76 | return new self('mixed', new Set\Primitive('mixed', ...$values)); |
||
| 77 | } |
||
| 78 | 1 | ||
| 79 | /** |
||
| 80 | * @return self<int> |
||
| 81 | 1 | */ |
|
| 82 | public static function ints(int ...$values): self |
||
| 83 | 1 | { |
|
| 84 | /** @var self<int> */ |
||
| 85 | $self = new self('int', new Set\Primitive('int', ...$values)); |
||
| 86 | |||
| 87 | return $self; |
||
| 88 | } |
||
| 89 | 1 | ||
| 90 | /** |
||
| 91 | * @return self<float> |
||
| 92 | 1 | */ |
|
| 93 | public static function floats(float ...$values): self |
||
| 94 | 1 | { |
|
| 95 | /** @var self<float> */ |
||
| 96 | $self = new self('float', new Set\Primitive('float', ...$values)); |
||
| 97 | |||
| 98 | return $self; |
||
| 99 | } |
||
| 100 | 1 | ||
| 101 | /** |
||
| 102 | * @return self<string> |
||
| 103 | 1 | */ |
|
| 104 | public static function strings(string ...$values): self |
||
| 105 | 1 | { |
|
| 106 | /** @var self<string> */ |
||
| 107 | $self = new self('string', new Set\Primitive('string', ...$values)); |
||
| 108 | 22 | ||
| 109 | return $self; |
||
| 110 | 22 | } |
|
| 111 | |||
| 112 | /** |
||
| 113 | * @return self<object> |
||
| 114 | */ |
||
| 115 | public static function objects(object ...$values): self |
||
| 116 | 24 | { |
|
| 117 | /** @var self<object> */ |
||
| 118 | 24 | $self = new self('object', new Set\Primitive('object', ...$values)); |
|
| 119 | |||
| 120 | return $self; |
||
| 121 | 1 | } |
|
| 122 | |||
| 123 | 1 | public function isOfType(string $type): bool |
|
| 124 | { |
||
| 125 | return $this->type === $type; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | 1 | * Return the type of this set |
|
| 130 | */ |
||
| 131 | 1 | public function type(): string |
|
| 132 | { |
||
| 133 | return $this->type; |
||
| 134 | } |
||
| 135 | |||
| 136 | public function size(): int |
||
| 137 | { |
||
| 138 | return $this->implementation->size(); |
||
| 139 | } |
||
| 140 | |||
| 141 | 2 | /** |
|
| 142 | * {@inheritdoc} |
||
| 143 | 2 | */ |
|
| 144 | public function count(): int |
||
| 145 | 1 | { |
|
| 146 | 1 | return $this->implementation->size(); |
|
| 147 | 1 | } |
|
| 148 | |||
| 149 | /** |
||
| 150 | 1 | * Intersect this set with the given one |
|
| 151 | * |
||
| 152 | * @param self<T> $set |
||
| 153 | * |
||
| 154 | * @return self<T> |
||
| 155 | */ |
||
| 156 | public function intersect(self $set): self |
||
| 157 | { |
||
| 158 | assertSet($this->type, $set, 1); |
||
| 159 | |||
| 160 | 46 | $newSet = clone $this; |
|
| 161 | $newSet->implementation = $this->implementation->intersect( |
||
| 162 | 46 | $set->implementation, |
|
| 163 | ); |
||
| 164 | 45 | ||
| 165 | 45 | return $newSet; |
|
| 166 | } |
||
| 167 | 45 | ||
| 168 | /** |
||
| 169 | * Add an element to the set |
||
| 170 | * |
||
| 171 | * @param T $element |
||
| 172 | * |
||
| 173 | * @return self<T> |
||
| 174 | */ |
||
| 175 | public function add($element): self |
||
| 176 | { |
||
| 177 | return ($this)($element); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Add an element to the set |
||
| 182 | * |
||
| 183 | 31 | * Example: |
|
| 184 | * <code> |
||
| 185 | 31 | * Set::of('int')(1)(3) |
|
| 186 | * </code> |
||
| 187 | * |
||
| 188 | * @param T $element |
||
| 189 | * |
||
| 190 | * @return self<T> |
||
| 191 | */ |
||
| 192 | public function __invoke($element): self |
||
| 193 | 1 | { |
|
| 194 | ($this->validate)($element, 1); |
||
| 195 | 1 | ||
| 196 | $self = clone $this; |
||
| 197 | 1 | $self->implementation = ($this->implementation)($element); |
|
| 198 | |||
| 199 | return $self; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Check if the set contains the given element |
||
| 204 | * |
||
| 205 | * @param T $element |
||
| 206 | */ |
||
| 207 | 1 | public function contains($element): bool |
|
| 208 | { |
||
| 209 | 1 | ($this->validate)($element, 1); |
|
| 210 | |||
| 211 | 1 | return $this->implementation->contains($element); |
|
| 212 | 1 | } |
|
| 213 | |||
| 214 | 1 | /** |
|
| 215 | * Remove the element from the set |
||
| 216 | * |
||
| 217 | * @param T $element |
||
| 218 | * |
||
| 219 | * @return self<T> |
||
| 220 | */ |
||
| 221 | public function remove($element): self |
||
| 222 | { |
||
| 223 | ($this->validate)($element, 1); |
||
| 224 | 2 | ||
| 225 | $self = clone $this; |
||
| 226 | 2 | $self->implementation = $this->implementation->remove($element); |
|
| 227 | |||
| 228 | 1 | return $self; |
|
| 229 | 1 | } |
|
| 230 | 1 | ||
| 231 | /** |
||
| 232 | * Return the diff between this set and the given one |
||
| 233 | 1 | * |
|
| 234 | * @param self<T> $set |
||
| 235 | * |
||
| 236 | * @return self<T> |
||
| 237 | */ |
||
| 238 | public function diff(self $set): self |
||
| 239 | { |
||
| 240 | assertSet($this->type, $set, 1); |
||
| 241 | 10 | ||
| 242 | $self = clone $this; |
||
| 243 | 10 | $self->implementation = $this->implementation->diff( |
|
| 244 | $set->implementation, |
||
| 245 | 9 | ); |
|
| 246 | |||
| 247 | return $self; |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Check if the given set is identical to this one |
||
| 252 | * |
||
| 253 | * @param self<T> $set |
||
| 254 | */ |
||
| 255 | 1 | public function equals(self $set): bool |
|
| 256 | { |
||
| 257 | 1 | assertSet($this->type, $set, 1); |
|
| 258 | 1 | ||
| 259 | return $this->implementation->equals($set->implementation); |
||
| 260 | 1 | } |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Return all elements that satisfy the given predicate |
||
| 264 | * |
||
| 265 | * @param callable(T): bool $predicate |
||
| 266 | * |
||
| 267 | * @return self<T> |
||
| 268 | 1 | */ |
|
| 269 | public function filter(callable $predicate): self |
||
| 270 | 1 | { |
|
| 271 | 1 | $set = clone $this; |
|
| 272 | $set->implementation = $this->implementation->filter($predicate); |
||
| 273 | |||
| 274 | return $set; |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Apply the given function to all elements of the set |
||
| 279 | * |
||
| 280 | * @param callable(T): void $function |
||
| 281 | */ |
||
| 282 | public function foreach(callable $function): void |
||
| 283 | { |
||
| 284 | 1 | $this->implementation->foreach($function); |
|
| 285 | } |
||
| 286 | 1 | ||
| 287 | /** |
||
| 288 | * Return a new map of pairs grouped by keys determined with the given |
||
| 289 | * discriminator function |
||
| 290 | * |
||
| 291 | * @template D |
||
| 292 | * @param callable(T): D $discriminator |
||
| 293 | * |
||
| 294 | * @throws CannotGroupEmptyStructure |
||
| 295 | * |
||
| 296 | 2 | * @return Map<D, self<T>> |
|
| 297 | */ |
||
| 298 | 2 | public function groupBy(callable $discriminator): Map |
|
| 299 | 2 | { |
|
| 300 | return $this->implementation->groupBy($discriminator); |
||
| 301 | 1 | } |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Return a new set by applying the given function to all elements |
||
| 305 | * |
||
| 306 | * @param callable(T): T $function |
||
| 307 | * |
||
| 308 | * @return self<T> |
||
| 309 | */ |
||
| 310 | public function map(callable $function): self |
||
| 311 | 1 | { |
|
| 312 | $self = $this->clear(); |
||
| 313 | 1 | $self->implementation = $this->implementation->map($function); |
|
| 314 | |||
| 315 | return $self; |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Create a new Set with the exact same number of elements but with a |
||
| 320 | * new type transformed via the given function |
||
| 321 | * |
||
| 322 | * @template S |
||
| 323 | 1 | * |
|
| 324 | * @param callable(T): S $map |
||
| 325 | 1 | * |
|
| 326 | * @return self<S> |
||
| 327 | */ |
||
| 328 | public function mapTo(string $type, callable $map): self |
||
| 329 | { |
||
| 330 | /** @psalm-suppress MixedArgument */ |
||
| 331 | return $this->toSetOf( |
||
| 332 | $type, |
||
| 333 | static fn($value): \Generator => yield $map($value), |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 334 | ); |
||
| 335 | 2 | } |
|
| 336 | |||
| 337 | 2 | /** |
|
| 338 | * Return a sequence of 2 sets partitioned according to the given predicate |
||
| 339 | 1 | * |
|
| 340 | 1 | * @param callable(T): bool $predicate |
|
| 341 | 1 | * |
|
| 342 | * @return Map<bool, self<T>> |
||
| 343 | */ |
||
| 344 | 1 | public function partition(callable $predicate): Map |
|
| 345 | { |
||
| 346 | return $this->implementation->partition($predicate); |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Return a sequence sorted with the given function |
||
| 351 | * |
||
| 352 | * @param callable(T, T): int $function |
||
| 353 | * |
||
| 354 | * @return Sequence<T> |
||
| 355 | */ |
||
| 356 | 60 | public function sort(callable $function): Sequence |
|
| 357 | { |
||
| 358 | 60 | return $this->implementation->sort($function); |
|
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Create a new set with elements of both sets |
||
| 363 | * |
||
| 364 | * @param self<T> $set |
||
| 365 | * |
||
| 366 | 2 | * @return self<T> |
|
| 367 | */ |
||
| 368 | 2 | public function merge(self $set): self |
|
| 369 | 2 | { |
|
| 370 | assertSet($this->type, $set, 1); |
||
| 371 | 2 | ||
| 372 | $self = clone $this; |
||
| 373 | $self->implementation = $this->implementation->merge( |
||
| 374 | 3 | $set->implementation, |
|
| 375 | ); |
||
| 376 | 3 | ||
| 377 | return $self; |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Reduce the set to a single value |
||
| 382 | * |
||
| 383 | * @template R |
||
| 384 | * @param R $carry |
||
| 385 | * @param callable(R, T): R $reducer |
||
| 386 | 1 | * |
|
| 387 | * @return R |
||
| 388 | 1 | */ |
|
| 389 | public function reduce($carry, callable $reducer) |
||
| 390 | { |
||
| 391 | return $this->implementation->reduce($carry, $reducer); |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Return a set of the same type but without any value |
||
| 396 | * |
||
| 397 | * @return self<T> |
||
| 398 | 1 | */ |
|
| 399 | public function clear(): self |
||
| 400 | 1 | { |
|
| 401 | $self = clone $this; |
||
| 402 | $self->implementation = $this->implementation->clear(); |
||
| 403 | |||
| 404 | return $self; |
||
| 405 | } |
||
| 406 | |||
| 407 | public function empty(): bool |
||
| 408 | { |
||
| 409 | return $this->implementation->empty(); |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | 1 | * @template ST |
|
| 414 | * |
||
| 415 | 1 | * @param null|callable(T): \Generator<ST> $mapper |
|
| 416 | * |
||
| 417 | * @return Sequence<ST> |
||
| 418 | */ |
||
| 419 | public function toSequenceOf(string $type, callable $mapper = null): Sequence |
||
| 420 | { |
||
| 421 | return $this->implementation->toSequenceOf($type, $mapper); |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @template ST |
||
| 426 | * |
||
| 427 | * @param null|callable(T): \Generator<ST> $mapper |
||
| 428 | * |
||
| 429 | * @return self<ST> |
||
| 430 | */ |
||
| 431 | public function toSetOf(string $type, callable $mapper = null): self |
||
| 432 | { |
||
| 433 | return $this->implementation->toSetOf($type, $mapper); |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @template MT |
||
| 438 | * @template MS |
||
| 439 | * |
||
| 440 | * @param callable(T): \Generator<MT, MS> $mapper |
||
| 441 | * |
||
| 442 | * @return Map<MT, MS> |
||
| 443 | */ |
||
| 444 | public function toMapOf(string $key, string $value, callable $mapper): Map |
||
| 445 | { |
||
| 446 | return $this->implementation->toMapOf($key, $value, $mapper); |
||
| 447 | } |
||
| 448 | } |
||
| 449 |