HH\Map::lazy

Returns a lazy, access elements only when needed view of the current Map

public function lazy(): HH\Rx\KeyedIterable<Tk, Tv>;

Normally, memory is allocated for all of the elements of the Map. 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<Tk, Tv> - a KeyedIterable representing the lazy view into the current Map.

Examples

This example shows you how to use lazy() on a rather large Map 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.

$map = new Map(range(0, 1000000));

$s = microtime(true);
$non_lazy = $map->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 Map can save us a bunch of time, possibly even
// cutting this call time by 90%.
$s = microtime(true);
$lazy = $map->lazy()->filter($x ==> $x % 2 === 0)->take(5);
$e = microtime(true);

var_dump($lazy->toMap());
echo "Time lazy: ".strval($e - $s).PHP_EOL;
Output
object(HH\Map)#4 (5) {
  [0]=>
  int(0)
  [2]=>
  int(2)
  [4]=>
  int(4)
  [6]=>
  int(6)
  [8]=>
  int(8)
}
Time non-lazy: 0.30753803253174
object(HH\Map)#9 (5) {
  [0]=>
  int(0)
  [2]=>
  int(2)
  [4]=>
  int(4)
  [6]=>
  int(6)
  [8]=>
  int(8)
}
Time lazy: 0.013100862503052