Skip to content

Commit f149513

Browse files
committed
refactor(underscore): add docblock, reorganize
1 parent 8247aa7 commit f149513

File tree

1 file changed

+45
-47
lines changed

1 file changed

+45
-47
lines changed

src/Underscore.php

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,75 @@
22

33
namespace Ahc\Underscore;
44

5-
final class Underscore extends UnderscoreArray
5+
final class Underscore extends UnderscoreFunction
66
{
77
/**
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.
109
*
11-
* @param callable $fn The main callback.
10+
* @param mixed $value
1211
*
13-
* @return mixed
12+
* @return callable
1413
*/
15-
public function memoize(callable $fn)
14+
public function constant($value)
1615
{
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;
2718
};
2819
}
2920

3021
/**
31-
* Cache the result of callback for given arguments and reuse that in subsequent call.
22+
* No operation!
3223
*
33-
* @param callable $fn The main callback.
34-
* @param int $wait The time to wait in millisec.
35-
*
36-
* @return mixed
24+
* @return void
3725
*/
38-
public function delay(callable $fn, $wait)
26+
public function noop()
3927
{
40-
return function () use ($fn, $wait) {
41-
\usleep(1000 * $wait);
42-
43-
return \call_user_func_array($fn, \func_get_args());
44-
};
28+
// ;)
4529
}
4630

4731
/**
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.
5033
*
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
5336
*
54-
* @return mixed The return set of callback if runnable else the previous cache.
37+
* @return self
5538
*/
56-
public function throttle(callable $fn, $wait)
39+
public function times($n, callable $fn)
5740
{
58-
static $previous = 0;
59-
static $result = null;
41+
$data = [];
6042

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+
}
6346

64-
if (!$previous) {
65-
$previous = $now;
66-
}
47+
return new static($data);
48+
}
6749

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+
}
6962

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;
7473

75-
return $result;
76-
};
74+
return $prefix . (++$id);
7775
}
7876
}

0 commit comments

Comments
 (0)