function _memoize($function, $hashFunction = NULL, &$cache = NULL) { return Underscore::memoize($function, $hashFunction, $cache); }
/** * @tags functions */ public function testMemoize() { // it should return a memoized version of the function $fn = function () { static $count = 0; return ++$count; }; $memo = _::memoize($fn); for ($i = 0; $i < 3; $i++, $memo()) { } $this->integer($memo())->isEqualTo(1); // it should be possible to override the hash function $hash = function ($args) { return array_sum($args); }; $fn = function ($a, $b, $c) { return $a + $b + $c; }; $memo = _::memoize($fn, $hash); $res[] = $memo(1, 2, 3); $res[] = $memo(1, 4, 5); // is different from $a because hash function uses all arguments $res[] = $memo(1, 6, 7); // is different from $a because hash function uses all arguments $this->array($res)->isEqualTo([6, 10, 14]); // it should be possible to override the cache $cache = []; $fn = function ($a) { return $a * $a; }; $memo = _::memoize($fn, null, $cache); $memo(1); // 1 $memo(2); // 4 $memo(3); // 9 $this->array($cache)->isEqualTo([1 => 1, 2 => 4, 3 => 9]); // it should throw an InvalidArgumentException if cache is not suitable $this->exception(function () { $cache = "hello"; _::memoize(function () { }, null, $cache); })->isInstanceOf('\\InvalidArgumentException'); }