|
| 1 | +--TEST-- |
| 2 | +Concrete example of using AT |
| 3 | +--CREDITS-- |
| 4 | +Levi Morrison |
| 5 | +--FILE-- |
| 6 | +<?php declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Sequence; |
| 9 | + |
| 10 | +interface Sequence |
| 11 | +{ |
| 12 | + // No null. This is probably going to be painful, but let's try it. |
| 13 | + type Item: object|array|string|float|int|bool; |
| 14 | + |
| 15 | + function next(): ?Item; |
| 16 | + |
| 17 | + /** |
| 18 | + * @param callable(Item, Item): Item $f |
| 19 | + * @return ?Item |
| 20 | + */ |
| 21 | + function reduce(callable $f): ?Item; |
| 22 | +} |
| 23 | + |
| 24 | +final class StringTablePair |
| 25 | +{ |
| 26 | + public function __construct( |
| 27 | + public readonly string $string, |
| 28 | + public readonly int $id, |
| 29 | + ) {} |
| 30 | +} |
| 31 | + |
| 32 | +final class StringTableSequence implements Sequence |
| 33 | +{ |
| 34 | + private array $strings; |
| 35 | + |
| 36 | + public function __construct(StringTable $string_table) { |
| 37 | + $this->strings = $string_table->to_assoc_array(); |
| 38 | + } |
| 39 | + |
| 40 | + function next(): ?StringTablePair |
| 41 | + { |
| 42 | + $key = \array_key_first($this->strings); |
| 43 | + if (!isset($key)) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + $value = \array_shift($this->strings); |
| 47 | + return new StringTablePair($key, $value); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @param callable(Item, Item): Item $f |
| 52 | + * @return ?Item |
| 53 | + */ |
| 54 | + function reduce(callable $f): ?StringTablePair |
| 55 | + { |
| 56 | + $reduction = $this->next(); |
| 57 | + if (!isset($reduction)) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + while (($next = $this->next()) !== null) { |
| 62 | + $reduction = $f($reduction, $next); |
| 63 | + } |
| 64 | + return $reduction; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +final class StringTable |
| 69 | +{ |
| 70 | + private array $strings = ["" => 0]; |
| 71 | + |
| 72 | + public function __construct() {} |
| 73 | + |
| 74 | + public function offsetGet(string $offset): int |
| 75 | + { |
| 76 | + return $this->strings[$offset] ?? throw new \Exception(); |
| 77 | + } |
| 78 | + |
| 79 | + public function offsetExists(string $offset): bool |
| 80 | + { |
| 81 | + return \isset($this->strings[$offset]); |
| 82 | + } |
| 83 | + |
| 84 | + public function intern(string $str): int |
| 85 | + { |
| 86 | + return $this->strings[$str] |
| 87 | + ?? ($this->strings[$str] = \count($this->strings)); |
| 88 | + } |
| 89 | + |
| 90 | + public function to_sequence(): StringTableSequence |
| 91 | + { |
| 92 | + return new StringTableSequence($this); |
| 93 | + } |
| 94 | + |
| 95 | + public function to_assoc_array(): array { |
| 96 | + return $this->strings; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +?> |
| 101 | +--EXPECTF-- |
| 102 | +Fatal error: Declaration of Sequence\StringTableSequence::next(): ?Sequence\StringTablePair must be compatible with Sequence\Sequence::next(): Item<object|array|string|int|float|bool>|null in %s on line %d |
0 commit comments