HH\Vector::lazy
Returns a lazy, access-elements-only-when-needed view of the current
Vector
public function lazy(): HH\Rx\KeyedIterable<int, Tv>;
Normally, memory is allocated for all of the elements of the Vector
.
With a lazy view, memory is allocated for an element only when needed or
used in a calculation like in map()
or filter()
.
Guide
Returns
HH\Rx\KeyedIterable<int, Tv>
- An integer-keyedKeyedIterable
representing the lazy view into the currentVector
.
Examples
This example shows you how to use lazy()
on a rather large Vector
and the time for both a strict and non-strict version. Since we only need 5 of the elements in the end, the lazy view actually allows us to stop after we meet our required 5 without having to actually filter and allocate all 1000000 elements up front.
$vector = new Vector(range(0, 1000000));
$s = microtime(true);
$non_lazy = $vector->filter($x ==> $x % 2 === 0)->take(5);
$e = microtime(true);
var_dump($non_lazy);
echo "Time non-lazy: ".strval($e - $s).PHP_EOL;
// Using a lazy view of the vector can save us a bunch of time, possibly even
// cutting this call time by 90%.
$s = microtime(true);
$lazy = $vector->lazy()->filter($x ==> $x % 2 === 0)->take(5);
$e = microtime(true);
var_dump($lazy->toVector());
echo "Time lazy: ".strval($e - $s).PHP_EOL;
object(HH\Vector)#4 (5) {
[0]=>
int(0)
[1]=>
int(2)
[2]=>
int(4)
[3]=>
int(6)
[4]=>
int(8)
}
Time non-lazy: 0.27172183990479
object(HH\Vector)#9 (5) {
[0]=>
int(0)
[1]=>
int(2)
[2]=>
int(4)
[3]=>
int(6)
[4]=>
int(8)
}
Time lazy: 0.015867948532104