@@ -68,7 +68,7 @@ public function __invoke($element): Implementation
68
68
69
69
public function size (): int
70
70
{
71
- return $ this ->load ()->size ();
71
+ return $ this ->memoize ()->size ();
72
72
}
73
73
74
74
public function count (): int
@@ -184,15 +184,15 @@ public function dropEnd(int $size): Implementation
184
184
{
185
185
// this cannot be optimised as the whole generator needs to be loaded
186
186
// in order to know the elements to drop
187
- return $ this ->load ()->dropEnd ($ size );
187
+ return $ this ->memoize ()->dropEnd ($ size );
188
188
}
189
189
190
190
/**
191
191
* @param Implementation<T> $sequence
192
192
*/
193
193
public function equals (Implementation $ sequence ): bool
194
194
{
195
- return $ this ->load ()->equals ($ sequence );
195
+ return $ this ->memoize ()->equals ($ sequence );
196
196
}
197
197
198
198
/**
@@ -241,7 +241,7 @@ public function foreach(callable $function): SideEffect
241
241
public function groupBy (callable $ discriminator ): Map
242
242
{
243
243
/** @var Map<D, Sequence<T>> */
244
- return $ this ->load ()->groupBy ($ discriminator );
244
+ return $ this ->memoize ()->groupBy ($ discriminator );
245
245
}
246
246
247
247
/**
@@ -453,7 +453,7 @@ public function pad(int $size, $element): Implementation
453
453
public function partition (callable $ predicate ): Map
454
454
{
455
455
/** @var Map<bool, Sequence<T>> */
456
- return $ this ->load ()->partition ($ predicate );
456
+ return $ this ->memoize ()->partition ($ predicate );
457
457
}
458
458
459
459
/**
@@ -516,7 +516,7 @@ public function takeEnd(int $size): Implementation
516
516
{
517
517
// this cannot be optimised as the whole generator needs to be loaded
518
518
// in order to know the elements to drop
519
- return $ this ->load ()->takeEnd ($ size );
519
+ return $ this ->memoize ()->takeEnd ($ size );
520
520
}
521
521
522
522
/**
@@ -835,7 +835,13 @@ public function aggregate(callable $map, callable $exfiltrate): self
835
835
*/
836
836
public function memoize (): Implementation
837
837
{
838
- return $ this ->load ();
838
+ $ values = [];
839
+
840
+ foreach ($ this ->values as $ value ) {
841
+ $ values [] = $ value ;
842
+ }
843
+
844
+ return new Primitive ($ values );
839
845
}
840
846
841
847
/**
@@ -907,20 +913,6 @@ public function takeWhile(callable $condition): self
907
913
);
908
914
}
909
915
910
- /**
911
- * @return Implementation<T>
912
- */
913
- private function load (): Implementation
914
- {
915
- $ values = [];
916
-
917
- foreach ($ this ->values as $ value ) {
918
- $ values [] = $ value ;
919
- }
920
-
921
- return new Primitive ($ values );
922
- }
923
-
924
916
/**
925
917
* @return array{\WeakReference<self<T>>, \Iterator<T>}
926
918
*/
0 commit comments