|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ahc\Underscore; |
| 4 | + |
| 5 | +class UnderscoreFunction extends UnderscoreArray |
| 6 | +{ |
| 7 | + /** |
| 8 | + * Returns a callable which when invoked caches the result for given arguments |
| 9 | + * and reuses that result in subsequent calls. |
| 10 | + * |
| 11 | + * @param callable $fn The main callback. |
| 12 | + * |
| 13 | + * @return mixed |
| 14 | + */ |
| 15 | + public function memoize(callable $fn) |
| 16 | + { |
| 17 | + static $memo = []; |
| 18 | + |
| 19 | + return function () use (&$memo, $fn) { |
| 20 | + $hash = \md5(\json_encode($args = \func_get_args())); |
| 21 | + |
| 22 | + if (isset($memo[$hash])) { |
| 23 | + return $memo[$hash]; |
| 24 | + } |
| 25 | + |
| 26 | + return $memo[$hash] = \call_user_func_array($fn, $args); |
| 27 | + }; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Cache the result of callback for given arguments and reuse that in subsequent call. |
| 32 | + * |
| 33 | + * @param callable $fn The main callback. |
| 34 | + * @param int $wait The time to wait in millisec. |
| 35 | + * |
| 36 | + * @return mixed |
| 37 | + */ |
| 38 | + public function delay(callable $fn, $wait) |
| 39 | + { |
| 40 | + return function () use ($fn, $wait) { |
| 41 | + \usleep(1000 * $wait); |
| 42 | + |
| 43 | + return \call_user_func_array($fn, \func_get_args()); |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Returns a callable that wraps given callable which can be only invoked |
| 49 | + * at most once per given $wait threshold. |
| 50 | + * |
| 51 | + * @param callable $fn The main callback. |
| 52 | + * @param int $wait The callback will only be triggered at most once within this period. |
| 53 | + * |
| 54 | + * @return mixed The return set of callback if runnable else the previous cache. |
| 55 | + */ |
| 56 | + public function throttle(callable $fn, $wait) |
| 57 | + { |
| 58 | + static $previous = 0; |
| 59 | + static $result = null; |
| 60 | + |
| 61 | + return function () use ($fn, &$previous, &$result, &$wait) { |
| 62 | + $now = $this->now(); |
| 63 | + |
| 64 | + if (!$previous) { |
| 65 | + $previous = $now; |
| 66 | + } |
| 67 | + |
| 68 | + $remaining = $wait - ($now - $previous); |
| 69 | + |
| 70 | + if ($remaining <= 0 || $remaining > $wait) { |
| 71 | + $previous = $now; |
| 72 | + $result = \call_user_func_array($fn, \func_get_args()); |
| 73 | + } |
| 74 | + |
| 75 | + return $result; |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Returns a function that is the composition of a list of functions, |
| 81 | + * each consuming the return value of the function that follows. |
| 82 | + * |
| 83 | + * Note that last function is executed first. |
| 84 | + * |
| 85 | + * @param callable $fn1 |
| 86 | + * @param callable $fn2 |
| 87 | + * @param ...callable|null $fn3 And so on! |
| 88 | + * |
| 89 | + * @return mixed Final result value. |
| 90 | + */ |
| 91 | + public function compose(callable $fn1, callable $fn2 /* , callable $fn3 = null */) |
| 92 | + { |
| 93 | + $fns = \func_get_args(); |
| 94 | + $start = \func_num_args() - 1; |
| 95 | + |
| 96 | + return function () use ($fns, $start) { |
| 97 | + $i = $start; |
| 98 | + $result = \call_user_func_array($fns[$start], \func_get_args()); |
| 99 | + |
| 100 | + while ($i--) { |
| 101 | + $result = $fns[$i]($result); |
| 102 | + } |
| 103 | + |
| 104 | + return $result; |
| 105 | + }; |
| 106 | + } |
| 107 | +} |
0 commit comments