@@ -20,12 +20,48 @@ abstract class Tuple implements \ArrayAccess
2020{
2121 public const TUPLE_MAX_SIZE = 8 ;
2222
23+ /**
24+ * @param mixed ...$values
25+ */
26+ public static function of (...$ values )
27+ {
28+ return match (count ($ values )) {
29+ 0 => new Tuple0 (),
30+ 1 => new Tuple1 (...$ values ),
31+ 2 => new Tuple2 (...$ values ),
32+ 3 => new Tuple3 (...$ values ),
33+ 4 => new Tuple4 (...$ values ),
34+ 5 => new Tuple5 (...$ values ),
35+ 6 => new Tuple6 (...$ values ),
36+ 7 => new Tuple7 (...$ values ),
37+ 8 => new Tuple8 (...$ values ),
38+ default => throw new \InvalidArgumentException ('Invalid number of elements ' ),
39+ };
40+ }
41+
2342 abstract public function arity (): int ;
2443
2544 abstract public function toArray (): array ;
2645
2746 abstract public function concat ($ tuple );
2847
48+ /**
49+ * @template U
50+ *
51+ * @param callable():U $transformer
52+ *
53+ * @return U
54+ */
55+ public function apply (callable $ transformer )
56+ {
57+ return call_user_func ($ transformer , ...$ this ->toArray ());
58+ }
59+
60+ public function map (callable $ mapper ): self
61+ {
62+ return self ::of (...array_map ($ mapper , $ this ->toArray ()));
63+ }
64+
2965 public function offsetExists (mixed $ offset ): bool
3066 {
3167 return isset ($ this ->toArray ()[$ offset ]);
@@ -51,39 +87,6 @@ public function offsetUnset(mixed $offset): void
5187 throw new UnsupportedOperationException ('cannot unset Tuple value ' );
5288 }
5389
54- /**
55- * @template U
56- *
57- * @param callable():U $transformer
58- *
59- * @return U
60- */
61- public function apply (callable $ transformer )
62- {
63- return call_user_func ($ transformer , ...$ this ->toArray ());
64- }
65-
66- public function map (callable $ mapper ): self
67- {
68- return self ::of (...array_map ($ mapper , $ this ->toArray ()));
69- }
70-
71- public static function of (...$ values )
72- {
73- return match (count ($ values )) {
74- 0 => new Tuple0 (),
75- 1 => new Tuple1 (...$ values ),
76- 2 => new Tuple2 (...$ values ),
77- 3 => new Tuple3 (...$ values ),
78- 4 => new Tuple4 (...$ values ),
79- 5 => new Tuple5 (...$ values ),
80- 6 => new Tuple6 (...$ values ),
81- 7 => new Tuple7 (...$ values ),
82- 8 => new Tuple8 (...$ values ),
83- default => throw new \InvalidArgumentException ('Invalid number of elements ' ),
84- };
85- }
86-
8790 public function equals (Tuple $ tuple ): bool
8891 {
8992 if ($ this ->arity () !== $ tuple ->arity ()) {
0 commit comments