-
Notifications
You must be signed in to change notification settings - Fork 1
/
Reductions.php
111 lines (104 loc) · 4.26 KB
/
Reductions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
* @copyright Zicht Online <https://zicht.nl>
*/
namespace Zicht\Itertools\util;
class Reductions
{
/**
* Returns a closure that adds two numbers together
*/
public static function add(): \Closure
{
return function ($a, $b) {
if (!is_numeric($a)) {
throw new \InvalidArgumentException(sprintf('Argument $A must be numeric to perform addition, not %s', is_object($a) ? get_class($a) : gettype($a)));
}
if (!is_numeric($b)) {
throw new \InvalidArgumentException(sprintf('Argument $B must be numeric to perform addition, not %s', is_object($b) ? get_class($b) : gettype($b)));
}
return $a + $b;
};
}
/**
* Returns a closure that subtracts one number from another
*/
public static function sub(): \Closure
{
return function ($a, $b) {
if (!is_numeric($a)) {
throw new \InvalidArgumentException(sprintf('Argument $A must be numeric to perform subtraction, not %s', is_object($a) ? get_class($a) : gettype($a)));
}
if (!is_numeric($b)) {
throw new \InvalidArgumentException(sprintf('Argument $B must be numeric to perform subtraction, not %s', is_object($b) ? get_class($b) : gettype($b)));
}
return $a - $b;
};
}
/**
* Returns a closure that multiplies two numbers
*/
public static function mul(): \Closure
{
return function ($a, $b) {
if (!is_numeric($a)) {
throw new \InvalidArgumentException(sprintf('Argument $A must be numeric to perform multiplication, not %s', is_object($a) ? get_class($a) : gettype($a)));
}
if (!is_numeric($b)) {
throw new \InvalidArgumentException(sprintf('Argument $B must be numeric to perform multiplication, not %s', is_object($b) ? get_class($b) : gettype($b)));
}
return $a * $b;
};
}
/**
* Returns a closure that returns the smallest of two numbers
*/
public static function min(): \Closure
{
return function ($a, $b) {
if (!(is_numeric($a) || $a instanceof \DateTime)) {
throw new \InvalidArgumentException(sprintf('Argument $A must be numeric to determine minimum, not %s', is_object($a) ? get_class($a) : gettype($a)));
}
if (!(is_numeric($b) || $b instanceof \DateTime)) {
throw new \InvalidArgumentException(sprintf('Argument $B must be numeric to determine minimum, not %s', is_object($b) ? get_class($b) : gettype($b)));
}
return $a < $b ? $a : $b;
};
}
/**
* Returns a closure that returns the largest of two numbers
*/
public static function max(): \Closure
{
return function ($a, $b) {
if (!(is_numeric($a) || $a instanceof \DateTime)) {
throw new \InvalidArgumentException(sprintf('Argument $A must be numeric to determine maximum, not %s', is_object($a) ? get_class($a) : gettype($a)));
}
if (!(is_numeric($b) || $b instanceof \DateTime)) {
throw new \InvalidArgumentException(sprintf('Argument $B must be numeric to determine maximum, not %s', is_object($b) ? get_class($b) : gettype($b)));
}
return $a < $b ? $b : $a;
};
}
/**
* Returns a closure that concatenates two strings using $glue
*
* @param string $glue
* @return \Closure
*/
public static function join($glue = '')
{
if (!is_string($glue)) {
throw new \InvalidArgumentException(sprintf('Argument $GLUE must be a string to join, not %s', is_object($glue) ? get_class($glue) : gettype($glue)));
}
return function ($a, $b) use ($glue) {
if (!is_string($a)) {
throw new \InvalidArgumentException(sprintf('Argument $A must be a string to join, not %s', is_object($a) ? get_class($a) : gettype($a)));
}
if (!is_string($b)) {
throw new \InvalidArgumentException(sprintf('Argument $B must be a string to join, not %s', is_object($b) ? get_class($b) : gettype($b)));
}
return \join($glue, [$a, $b]);
};
}
}