|
1
|
|
|
<?php namespace Tarsana\Functional; |
|
2
|
|
|
/** |
|
3
|
|
|
* Basic Math functions. |
|
4
|
|
|
* @file |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Computes `$x + $y`. |
|
9
|
|
|
* |
|
10
|
|
|
* ```php |
|
11
|
|
|
* $plusTwo = F\plus(2); |
|
12
|
|
|
* $plusTwo(5); //=> 7 |
|
13
|
|
|
* ``` |
|
14
|
|
|
* |
|
15
|
|
|
* @stream |
|
16
|
|
|
* @signature Number -> Number -> Number |
|
17
|
|
|
* @param int|float $x |
|
|
|
|
|
|
18
|
|
|
* @param int|float $y |
|
|
|
|
|
|
19
|
|
|
* @return int|float |
|
20
|
|
|
*/ |
|
21
|
|
View Code Duplication |
function plus() { |
|
|
|
|
|
|
22
|
|
|
static $plus = false; |
|
23
|
|
|
$plus = $plus ?: curry(function($x, $y){ |
|
24
|
|
|
return $x + $y; |
|
25
|
|
|
}); |
|
26
|
|
|
return _apply($plus, func_get_args()); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Computues `$x - $y`. |
|
31
|
|
|
* |
|
32
|
|
|
* ```php |
|
33
|
|
|
* F\minus(7, 2); //=> 5 |
|
34
|
|
|
* ``` |
|
35
|
|
|
* |
|
36
|
|
|
* @stream |
|
37
|
|
|
* @signature Number -> Number -> Number |
|
38
|
|
|
* @param int|float $x |
|
|
|
|
|
|
39
|
|
|
* @param int|float $y |
|
|
|
|
|
|
40
|
|
|
* @return int|float |
|
41
|
|
|
*/ |
|
42
|
|
View Code Duplication |
function minus() { |
|
|
|
|
|
|
43
|
|
|
static $minus = false; |
|
44
|
|
|
$minus = $minus ?: curry(function($x, $y){ |
|
45
|
|
|
return $x - $y; |
|
46
|
|
|
}); |
|
47
|
|
|
return _apply($minus, func_get_args()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Computes `- $x`. |
|
52
|
|
|
* |
|
53
|
|
|
* ```php |
|
54
|
|
|
* F\negate(5); //=> -5 |
|
55
|
|
|
* F\negate(-7); //=> 7 |
|
56
|
|
|
* ``` |
|
57
|
|
|
* |
|
58
|
|
|
* @stream |
|
59
|
|
|
* @signature Number -> Number |
|
60
|
|
|
* @param int|float $x |
|
|
|
|
|
|
61
|
|
|
* @return int|float |
|
62
|
|
|
*/ |
|
63
|
|
View Code Duplication |
function negate() { |
|
|
|
|
|
|
64
|
|
|
static $negate = false; |
|
65
|
|
|
$negate = $negate ?: curry(function($x){ |
|
66
|
|
|
return -$x; |
|
67
|
|
|
}); |
|
68
|
|
|
return _apply($negate, func_get_args()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Computes `$x * $y`. |
|
73
|
|
|
* |
|
74
|
|
|
* ```php |
|
75
|
|
|
* $double = F\multiply(2); |
|
76
|
|
|
* $double(5); //=> 10 |
|
77
|
|
|
* ``` |
|
78
|
|
|
* |
|
79
|
|
|
* @stream |
|
80
|
|
|
* @signature Number -> Number -> Number |
|
81
|
|
|
* @param int|float $x |
|
|
|
|
|
|
82
|
|
|
* @param int|float $y |
|
|
|
|
|
|
83
|
|
|
* @return int|float |
|
84
|
|
|
*/ |
|
85
|
|
View Code Duplication |
function multiply() { |
|
|
|
|
|
|
86
|
|
|
static $multiply = false; |
|
87
|
|
|
$multiply = $multiply ?: curry(function($x, $y){ |
|
88
|
|
|
return $y * $x; |
|
89
|
|
|
}); |
|
90
|
|
|
return _apply($multiply, func_get_args()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Computes `$x / $y`. |
|
95
|
|
|
* |
|
96
|
|
|
* ```php |
|
97
|
|
|
* F\divide(10, 2); //=> 5 |
|
98
|
|
|
* ``` |
|
99
|
|
|
* |
|
100
|
|
|
* @stream |
|
101
|
|
|
* @signature Number -> Number -> Number |
|
102
|
|
|
* @param int|float $x |
|
|
|
|
|
|
103
|
|
|
* @param int|float $y |
|
|
|
|
|
|
104
|
|
|
* @return int|float |
|
105
|
|
|
*/ |
|
106
|
|
View Code Duplication |
function divide() { |
|
|
|
|
|
|
107
|
|
|
static $divide = false; |
|
108
|
|
|
$divide = $divide ?: curry(function($x, $y){ |
|
109
|
|
|
return $x / $y; |
|
110
|
|
|
}); |
|
111
|
|
|
return _apply($divide, func_get_args()); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Computes `$x % $y`. |
|
116
|
|
|
* |
|
117
|
|
|
* ```php |
|
118
|
|
|
* F\modulo(10, 3); //=> 1 |
|
119
|
|
|
* ``` |
|
120
|
|
|
* |
|
121
|
|
|
* @stream |
|
122
|
|
|
* @signature Number -> Number -> Number |
|
123
|
|
|
* @param int|float $x |
|
|
|
|
|
|
124
|
|
|
* @param int|float $y |
|
|
|
|
|
|
125
|
|
|
* @return int|float |
|
126
|
|
|
*/ |
|
127
|
|
View Code Duplication |
function modulo() { |
|
|
|
|
|
|
128
|
|
|
static $modulo = false; |
|
129
|
|
|
$modulo = $modulo ?: curry(function($x, $y){ |
|
130
|
|
|
return $x % $y; |
|
131
|
|
|
}); |
|
132
|
|
|
return _apply($modulo, func_get_args()); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Computes the sum of an array of numbers. |
|
137
|
|
|
* |
|
138
|
|
|
* ```php |
|
139
|
|
|
* F\sum([1, 2, 3, 4]); //=> 10 |
|
140
|
|
|
* F\sum([]); //=> 0 |
|
141
|
|
|
* ``` |
|
142
|
|
|
* |
|
143
|
|
|
* @stream |
|
144
|
|
|
* @signature [Number] -> Number |
|
145
|
|
|
* @param array $numbers |
|
|
|
|
|
|
146
|
|
|
* @return int|float |
|
147
|
|
|
*/ |
|
148
|
|
|
function sum() { |
|
149
|
|
|
static $sum = false; |
|
150
|
|
|
$sum = $sum ?: curry('array_sum'); |
|
151
|
|
|
return _apply($sum, func_get_args()); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Computes the product of an array of numbers. |
|
156
|
|
|
* |
|
157
|
|
|
* ```php |
|
158
|
|
|
* F\product([1, 2, 3, 4]); //=> 24 |
|
159
|
|
|
* F\product([]); //=> 1 |
|
160
|
|
|
* ``` |
|
161
|
|
|
* |
|
162
|
|
|
* @stream |
|
163
|
|
|
* @signature [Number] -> Number |
|
164
|
|
|
* @param array $numbers |
|
|
|
|
|
|
165
|
|
|
* @return int|float |
|
166
|
|
|
*/ |
|
167
|
|
|
function product() { |
|
168
|
|
|
static $product = false; |
|
169
|
|
|
$product = $product ?: curry('array_product'); |
|
170
|
|
|
return _apply($product, func_get_args()); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Computes the minimum of two numbers. |
|
175
|
|
|
* |
|
176
|
|
|
* ```php |
|
177
|
|
|
* F\min(1, 3); //=> 1 |
|
178
|
|
|
* F\min(1, -3); //=> -3 |
|
179
|
|
|
* ``` |
|
180
|
|
|
* |
|
181
|
|
|
* @stream |
|
182
|
|
|
* @signature Number -> Number -> Number |
|
183
|
|
|
* @param number $a |
|
|
|
|
|
|
184
|
|
|
* @param number $b |
|
|
|
|
|
|
185
|
|
|
* @return number |
|
186
|
|
|
*/ |
|
187
|
|
View Code Duplication |
function min() { |
|
|
|
|
|
|
188
|
|
|
static $min = false; |
|
189
|
|
|
$min = $min ?: curry(function($a, $b){ |
|
190
|
|
|
return $a < $b ? $a : $b; |
|
191
|
|
|
}); |
|
192
|
|
|
return _apply($min, func_get_args()); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Computes the minimum of two elements using a function. |
|
197
|
|
|
* |
|
198
|
|
|
* ```php |
|
199
|
|
|
* F\minBy(F\length(), 'Hello', 'Hi'); //=> 'Hi' |
|
200
|
|
|
* F\minBy('abs', 1, -3); //=> 1 |
|
201
|
|
|
* ``` |
|
202
|
|
|
* |
|
203
|
|
|
* @stream |
|
204
|
|
|
* @signature (a -> Number) -> a -> a -> a |
|
205
|
|
|
* @param callable $fn |
|
|
|
|
|
|
206
|
|
|
* @param mixed $a |
|
|
|
|
|
|
207
|
|
|
* @param mixed $b |
|
|
|
|
|
|
208
|
|
|
* @return mixed |
|
209
|
|
|
*/ |
|
210
|
|
View Code Duplication |
function minBy() { |
|
|
|
|
|
|
211
|
|
|
static $minBy = false; |
|
212
|
|
|
$minBy = $minBy ?: curry(function($fn, $a, $b){ |
|
213
|
|
|
return $fn($a) < $fn($b) ? $a : $b; |
|
214
|
|
|
}); |
|
215
|
|
|
return _apply($minBy, func_get_args()); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Computes the maximum of two numbers. |
|
220
|
|
|
* |
|
221
|
|
|
* ```php |
|
222
|
|
|
* F\max(1, 3); //=> 3 |
|
223
|
|
|
* F\max(1, -3); //=> 1 |
|
224
|
|
|
* ``` |
|
225
|
|
|
* |
|
226
|
|
|
* @stream |
|
227
|
|
|
* @signature Number -> Number -> Number |
|
228
|
|
|
* @param number $a |
|
|
|
|
|
|
229
|
|
|
* @param number $b |
|
|
|
|
|
|
230
|
|
|
* @return number |
|
231
|
|
|
*/ |
|
232
|
|
View Code Duplication |
function max() { |
|
|
|
|
|
|
233
|
|
|
static $max = false; |
|
234
|
|
|
$max = $max ?: curry(function($a, $b){ |
|
235
|
|
|
return $a > $b ? $a : $b; |
|
236
|
|
|
}); |
|
237
|
|
|
return _apply($max, func_get_args()); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Computes the maximum of two elements using a function. |
|
242
|
|
|
* |
|
243
|
|
|
* ```php |
|
244
|
|
|
* F\maxBy(F\length(), 'Hello', 'Hi'); //=> 'Hello' |
|
245
|
|
|
* F\maxBy('abs', 1, -3); //=> -3 |
|
246
|
|
|
* ``` |
|
247
|
|
|
* |
|
248
|
|
|
* @stream |
|
249
|
|
|
* @signature (a -> Number) -> a -> a -> a |
|
250
|
|
|
* @param callable $fn |
|
|
|
|
|
|
251
|
|
|
* @param mixed $a |
|
|
|
|
|
|
252
|
|
|
* @param mixed $b |
|
|
|
|
|
|
253
|
|
|
* @return mixed |
|
254
|
|
|
*/ |
|
255
|
|
|
function maxBy() { |
|
256
|
|
|
static $maxBy = false; |
|
257
|
|
|
$maxBy = $maxBy ?: curry(function($fn, $a, $b){ |
|
258
|
|
|
return $fn($a) > $fn($b) ? $a : $b; |
|
259
|
|
|
}); |
|
260
|
|
|
return _apply($maxBy, func_get_args()); |
|
261
|
|
|
} |
|
262
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.