Default Example
function memoize($func)
{
return function () use ($func) {
static $cache = [];
$args = func_get_args();
$key = serialize($args);
$cached = true;
if (!isset($cache[$key])) {
$cache[$key] = $func(...$args);
$cached = false;
}
return ['result' => $cache[$key], 'cached' => $cached];
};
}
$memoizedAdd = memoize(
function ($num) {
return $num + 10;
}
);
var_dump($memoizedAdd(5)); // ['result' => 15, 'cached' => false]
var_dump($memoizedAdd(6)); // ['result' => 16, 'cached' => false]
var_dump($memoizedAdd(5)); // ['result' => 15, 'cached' => true]
Dark Example
function memoize($func)
{
return function () use ($func) {
static $cache = [];
$args = func_get_args();
$key = serialize($args);
$cached = true;
if (!isset($cache[$key])) {
$cache[$key] = $func(...$args);
$cached = false;
}
return ['result' => $cache[$key], 'cached' => $cached];
};
}
$memoizedAdd = memoize(
function ($num) {
return $num + 10;
}
);
var_dump($memoizedAdd(5)); // ['result' => 15, 'cached' => false]
var_dump($memoizedAdd(6)); // ['result' => 16, 'cached' => false]
var_dump($memoizedAdd(5)); // ['result' => 15, 'cached' => true]
Coy Example
function memoize($func)
{
return function () use ($func) {
static $cache = [];
$args = func_get_args();
$key = serialize($args);
$cached = true;
if (!isset($cache[$key])) {
$cache[$key] = $func(...$args);
$cached = false;
}
return ['result' => $cache[$key], 'cached' => $cached];
};
}
$memoizedAdd = memoize(
function ($num) {
return $num + 10;
}
);
var_dump($memoizedAdd(5)); // ['result' => 15, 'cached' => false]
var_dump($memoizedAdd(6)); // ['result' => 16, 'cached' => false]
var_dump($memoizedAdd(5)); // ['result' => 15, 'cached' => true]
Funky Example
function memoize($func)
{
return function () use ($func) {
static $cache = [];
$args = func_get_args();
$key = serialize($args);
$cached = true;
if (!isset($cache[$key])) {
$cache[$key] = $func(...$args);
$cached = false;
}
return ['result' => $cache[$key], 'cached' => $cached];
};
}
$memoizedAdd = memoize(
function ($num) {
return $num + 10;
}
);
var_dump($memoizedAdd(5)); // ['result' => 15, 'cached' => false]
var_dump($memoizedAdd(6)); // ['result' => 16, 'cached' => false]
var_dump($memoizedAdd(5)); // ['result' => 15, 'cached' => true]
Okaidia Example
function memoize($func)
{
return function () use ($func) {
static $cache = [];
$args = func_get_args();
$key = serialize($args);
$cached = true;
if (!isset($cache[$key])) {
$cache[$key] = $func(...$args);
$cached = false;
}
return ['result' => $cache[$key], 'cached' => $cached];
};
}
$memoizedAdd = memoize(
function ($num) {
return $num + 10;
}
);
var_dump($memoizedAdd(5)); // ['result' => 15, 'cached' => false]
var_dump($memoizedAdd(6)); // ['result' => 16, 'cached' => false]
var_dump($memoizedAdd(5)); // ['result' => 15, 'cached' => true]
Solarized Light Example
function memoize($func)
{
return function () use ($func) {
static $cache = [];
$args = func_get_args();
$key = serialize($args);
$cached = true;
if (!isset($cache[$key])) {
$cache[$key] = $func(...$args);
$cached = false;
}
return ['result' => $cache[$key], 'cached' => $cached];
};
}
$memoizedAdd = memoize(
function ($num) {
return $num + 10;
}
);
var_dump($memoizedAdd(5)); // ['result' => 15, 'cached' => false]
var_dump($memoizedAdd(6)); // ['result' => 16, 'cached' => false]
var_dump($memoizedAdd(5)); // ['result' => 15, 'cached' => true]
Tomorrow Night Example
function memoize($func)
{
return function () use ($func) {
static $cache = [];
$args = func_get_args();
$key = serialize($args);
$cached = true;
if (!isset($cache[$key])) {
$cache[$key] = $func(...$args);
$cached = false;
}
return ['result' => $cache[$key], 'cached' => $cached];
};
}
$memoizedAdd = memoize(
function ($num) {
return $num + 10;
}
);
var_dump($memoizedAdd(5)); // ['result' => 15, 'cached' => false]
var_dump($memoizedAdd(6)); // ['result' => 16, 'cached' => false]
var_dump($memoizedAdd(5)); // ['result' => 15, 'cached' => true]
Tonight Example
function memoize($func)
{
return function () use ($func) {
static $cache = [];
$args = func_get_args();
$key = serialize($args);
$cached = true;
if (!isset($cache[$key])) {
$cache[$key] = $func(...$args);
$cached = false;
}
return ['result' => $cache[$key], 'cached' => $cached];
};
}
$memoizedAdd = memoize(
function ($num) {
return $num + 10;
}
);
var_dump($memoizedAdd(5)); // ['result' => 15, 'cached' => false]
var_dump($memoizedAdd(6)); // ['result' => 16, 'cached' => false]
var_dump($memoizedAdd(5)); // ['result' => 15, 'cached' => true]