type num = int|float; final readonly class Collection { public function __construct( private vec $items = vec[], ) {} public function filter(Closure<(T), bool> $func): Collection { $result = vec[]; foreach $this->items as $item { if $func($item) { $result[] = $item; } } return new Collection::($result); } public function map(Closure<(T), Tout> $func): Collection { $result = vec[]; foreach $this->items as $item { $result[] = $func($item); } return new Collection::($result); } public function sum(): int where T is num { $result = 0; foreach $this->items as $item { $result += $item; } $result } } function example(): int { $collection = new Collection::(vec[ 1, 2, 3.0, 4.0, 5, 6, 7.0, 9.0, 10 ]); $collection ->filter(fn(num $n): bool => $n < 8) ->map::( fn(num $n): float => $n is float ? $n : $n + 0.0 ) ->sum() }