Skip to content
This repository was archived by the owner on Dec 28, 2023. It is now read-only.

Commit da5746a

Browse files
committed
Add CollectionProviderInterface::lazy()
1 parent d02bd22 commit da5746a

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

src/Enumerable.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -860,14 +860,15 @@ public function isEmpty()
860860
return Iterators::isEmpty($xs);
861861
}
862862

863-
private function newCollection($source)
863+
protected function newCollection($source)
864864
{
865865
return new Collection($source, $this->getProvider());
866866
}
867867

868-
private function newLazyCollection(callable $factory)
868+
protected function newLazyCollection(callable $factory)
869869
{
870-
return new Collection(Iterators::createLazy($factory), $this->getProvider());
870+
$provider = $this->getProvider();
871+
return new Collection($provider->lazy($factory), $provider);
871872
}
872873

873874
private function resolveComparer($comparer)

src/Provider/ArrayProvider.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,4 +374,12 @@ public function iterate($initial, callable $f)
374374
{
375375
throw new \OverflowException("Can't handle infinite stream.");
376376
}
377+
378+
/**
379+
* {@inheritdoc}
380+
*/
381+
public function lazy(callable $factory)
382+
{
383+
return $factory();
384+
}
377385
}

src/Provider/CollectionProviderInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,10 @@ public function renum($xs);
178178
* @return array|\Iterator
179179
*/
180180
public function iterate($initial, callable $f);
181+
182+
/**
183+
* @param callable $factory
184+
* @return array|\Iterator
185+
*/
186+
public function lazy(callable $factory);
181187
}

src/Provider/GeneratorProvider.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,12 @@ public function iterate($initial, callable $f)
451451
}
452452
});
453453
}
454+
455+
/**
456+
* {@inheritdoc}
457+
*/
458+
public function lazy(callable $factory)
459+
{
460+
return Iterators::createLazy($factory);
461+
}
454462
}

src/Provider/IteratorProvider.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,12 @@ public function iterate($initial, callable $f)
287287
{
288288
return new IterateIterator($initial, $f);
289289
}
290+
291+
/**
292+
* {@inheritdoc}
293+
*/
294+
public function lazy(callable $factory)
295+
{
296+
return Iterators::createLazy($factory);
297+
}
290298
}

0 commit comments

Comments
 (0)