Skip to content

Commit 6b059b3

Browse files
committed
move Sequence\Defer::load() to ::memoize()
1 parent 1ab3196 commit 6b059b3

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

src/Sequence/Defer.php

+13-21
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function __invoke($element): Implementation
6868

6969
public function size(): int
7070
{
71-
return $this->load()->size();
71+
return $this->memoize()->size();
7272
}
7373

7474
public function count(): int
@@ -184,15 +184,15 @@ public function dropEnd(int $size): Implementation
184184
{
185185
// this cannot be optimised as the whole generator needs to be loaded
186186
// in order to know the elements to drop
187-
return $this->load()->dropEnd($size);
187+
return $this->memoize()->dropEnd($size);
188188
}
189189

190190
/**
191191
* @param Implementation<T> $sequence
192192
*/
193193
public function equals(Implementation $sequence): bool
194194
{
195-
return $this->load()->equals($sequence);
195+
return $this->memoize()->equals($sequence);
196196
}
197197

198198
/**
@@ -241,7 +241,7 @@ public function foreach(callable $function): SideEffect
241241
public function groupBy(callable $discriminator): Map
242242
{
243243
/** @var Map<D, Sequence<T>> */
244-
return $this->load()->groupBy($discriminator);
244+
return $this->memoize()->groupBy($discriminator);
245245
}
246246

247247
/**
@@ -453,7 +453,7 @@ public function pad(int $size, $element): Implementation
453453
public function partition(callable $predicate): Map
454454
{
455455
/** @var Map<bool, Sequence<T>> */
456-
return $this->load()->partition($predicate);
456+
return $this->memoize()->partition($predicate);
457457
}
458458

459459
/**
@@ -516,7 +516,7 @@ public function takeEnd(int $size): Implementation
516516
{
517517
// this cannot be optimised as the whole generator needs to be loaded
518518
// in order to know the elements to drop
519-
return $this->load()->takeEnd($size);
519+
return $this->memoize()->takeEnd($size);
520520
}
521521

522522
/**
@@ -835,7 +835,13 @@ public function aggregate(callable $map, callable $exfiltrate): self
835835
*/
836836
public function memoize(): Implementation
837837
{
838-
return $this->load();
838+
$values = [];
839+
840+
foreach ($this->values as $value) {
841+
$values[] = $value;
842+
}
843+
844+
return new Primitive($values);
839845
}
840846

841847
/**
@@ -907,20 +913,6 @@ public function takeWhile(callable $condition): self
907913
);
908914
}
909915

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-
924916
/**
925917
* @return array{\WeakReference<self<T>>, \Iterator<T>}
926918
*/

0 commit comments

Comments
 (0)