|
2 | 2 |
|
3 | 3 | namespace Ahc\Underscore;
|
4 | 4 |
|
5 |
| -final class Underscore extends UnderscoreArray |
| 5 | +final class Underscore extends UnderscoreFunction |
6 | 6 | {
|
7 | 7 | /**
|
8 |
| - * Returns a callable which when invoked caches the result for given arguments |
9 |
| - * and reuses that result in subsequent calls. |
| 8 | + * Generates a function that always returns a constant value. |
10 | 9 | *
|
11 |
| - * @param callable $fn The main callback. |
| 10 | + * @param mixed $value |
12 | 11 | *
|
13 |
| - * @return mixed |
| 12 | + * @return callable |
14 | 13 | */
|
15 |
| - public function memoize(callable $fn) |
| 14 | + public function constant($value) |
16 | 15 | {
|
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); |
| 16 | + return function () use ($value) { |
| 17 | + return $value; |
27 | 18 | };
|
28 | 19 | }
|
29 | 20 |
|
30 | 21 | /**
|
31 |
| - * Cache the result of callback for given arguments and reuse that in subsequent call. |
| 22 | + * No operation! |
32 | 23 | *
|
33 |
| - * @param callable $fn The main callback. |
34 |
| - * @param int $wait The time to wait in millisec. |
35 |
| - * |
36 |
| - * @return mixed |
| 24 | + * @return void |
37 | 25 | */
|
38 |
| - public function delay(callable $fn, $wait) |
| 26 | + public function noop() |
39 | 27 | {
|
40 |
| - return function () use ($fn, $wait) { |
41 |
| - \usleep(1000 * $wait); |
42 |
| - |
43 |
| - return \call_user_func_array($fn, \func_get_args()); |
44 |
| - }; |
| 28 | + // ;) |
45 | 29 | }
|
46 | 30 |
|
47 | 31 | /**
|
48 |
| - * Returns a callable that wraps given callable which can be only invoked |
49 |
| - * at most once per given $wait threshold. |
| 32 | + * Run callable n times and create new collection. |
50 | 33 | *
|
51 |
| - * @param callable $fn The main callback. |
52 |
| - * @param int $wait The callback will only be triggered at most once within this period. |
| 34 | + * @param int $n |
| 35 | + * @param callable $fn |
53 | 36 | *
|
54 |
| - * @return mixed The return set of callback if runnable else the previous cache. |
| 37 | + * @return self |
55 | 38 | */
|
56 |
| - public function throttle(callable $fn, $wait) |
| 39 | + public function times($n, callable $fn) |
57 | 40 | {
|
58 |
| - static $previous = 0; |
59 |
| - static $result = null; |
| 41 | + $data = []; |
60 | 42 |
|
61 |
| - return function () use ($fn, &$previous, &$result, &$wait) { |
62 |
| - $now = $this->now(); |
| 43 | + for ($i = 0; $i < $n; $i++) { |
| 44 | + $data[$i] = $fn($i); |
| 45 | + } |
63 | 46 |
|
64 |
| - if (!$previous) { |
65 |
| - $previous = $now; |
66 |
| - } |
| 47 | + return new static($data); |
| 48 | + } |
67 | 49 |
|
68 |
| - $remaining = $wait - ($now - $previous); |
| 50 | + /** |
| 51 | + * Return a random integer between min and max (inclusive). |
| 52 | + * |
| 53 | + * @param int $min |
| 54 | + * @param int $max |
| 55 | + * |
| 56 | + * @return int |
| 57 | + */ |
| 58 | + public function random($min, $max) |
| 59 | + { |
| 60 | + return \mt_rand($min, $max); |
| 61 | + } |
69 | 62 |
|
70 |
| - if ($remaining <= 0 || $remaining > $wait) { |
71 |
| - $previous = $now; |
72 |
| - $result = \call_user_func_array($fn, \func_get_args()); |
73 |
| - } |
| 63 | + /** |
| 64 | + * Generate unique ID (unique for current go/session). |
| 65 | + * |
| 66 | + * @param string $prefix |
| 67 | + * |
| 68 | + * @return string |
| 69 | + */ |
| 70 | + public function uniqueId($prefix = '') |
| 71 | + { |
| 72 | + static $id = 0; |
74 | 73 |
|
75 |
| - return $result; |
76 |
| - }; |
| 74 | + return $prefix . (++$id); |
77 | 75 | }
|
78 | 76 | }
|
0 commit comments