@_kabal_
Here's two examples of how you can make this work with a standard function (your first example). Code comments are inline; but PM if you have any questions.
Solution 1 method signature (lazy):
Last lambda closure () is what delays the execution.
($key, $cache) -> ($hitcb, $misscb) -> () -> Void
Solution 2 method signature:
($key, $cache) -> ($hitcb, $misscb) -> Void
Note: the final Void return is based on the definition of the $hit / $miss lambda functions; which at the moment don't return anything; they just simply echo something. Naturally you can change this.
Here's two examples of how you can make this work with a standard function (your first example). Code comments are inline; but PM if you have any questions.
Solution 1 method signature (lazy):
Last lambda closure () is what delays the execution.
($key, $cache) -> ($hitcb, $misscb) -> () -> Void
Solution 2 method signature:
($key, $cache) -> ($hitcb, $misscb) -> Void
Note: the final Void return is based on the definition of the $hit / $miss lambda functions; which at the moment don't return anything; they just simply echo something. Naturally you can change this.
PHP:
<?php
// ---------------------------------------------------------------
// Summary: PHP allows functions within functions, ...
// This allows us to do step processes similar to what
// other languages like Haskell, F#, Swift, ... offer
// This is more typically known as currying, of which
// partial application is also an implementation.
//
// Partial application however prefills / calculates some of
// the parameters to be shared amongst calls. For example:
// Pre-initialising database functions with connection values.
//
// The examples I've included more closely mirror what
// what I believe you are doing, except that I given you two ways
// to do this; delayed execution (lazy), and immediate execution.
// ---------------------------------------------------------------
// This is the shared objects for this example; stand-in cache (dictionary),
// $hit / $miss lambda functions which can be replaced by inline closures.
// Dictionary (example's stand-in for cache)
$appcache = [
"foo" => 212,
"bar" => 323,
];
// Cache hit lambda callback function (alternate: inline closure)
$hit = function ($value) {
echo "cache hit; value = ".$value."\n";
};
// Cache miss lambda callback function (alternate: inline closure)
$miss = function () {
echo "cache miss\n";
};
// ---------------------------------------------------------------
// Solution 1: Deferred execution until later (lazy)
// Return: value from function call is the composed function
// wrapped within a lambda function that just needs
// to be called to evalute.
// Lazy execute result is value from $hit / $miss
// (i.e. void in this example)
// ---------------------------------------------------------------
function getCachedItem($key, $cache) {
return function ($hitcb, $misscb) use ($key, $cache) {
return function () use ($hitcb, $misscb, $key, $cache) {
array_key_exists($key, $cache) ? $hitcb($cache[$key]) : $misscb();
};
};
}
$foo = getCachedItem("foo", $appcache)($hit, $miss); // nothing happens here, $foo now holds the delayed function call for "foo"
$foos = getCachedItem("foo", $appcache)($hit, $miss); // nothing happens here, $foos now holds the delayed function call for "foos"
$result = [$foo, $foos];
array_walk($result, function ($callback) { $callback(); }); // Execution happens here, result is: "cache hit; value = 212" followed by "cache miss"
// ---------------------------------------------------------------
// Solution 2: Execute on call
// Return: Immediate execution of $miss / $hit, result is value
// $miss / $hit (i.e. void in this example)
// ---------------------------------------------------------------
function getCachedItem2($key, $cache) {
return function ($hitcb, $misscb) use ($key, $cache) {
array_key_exists($key, $cache)?$hitcb($cache[$key]):$misscb();
};
}
getCachedItem2("bar", $appcache)($hit, $miss); // output = "cache hit; value = 323"
getCachedItem2("bars", $appcache)($hit, $miss); // output = "cache miss"
?>
Last edited: