|
1
|
|
|
<?php namespace Tarsana\Functional; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file contains operators as functions. |
|
5
|
|
|
* @file |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Returns `$a && $b`. |
|
10
|
|
|
* |
|
11
|
|
|
* ```php |
|
12
|
|
|
* $isTrue = F\and_(true); |
|
13
|
|
|
* $isTrue(false); //=> false |
|
14
|
|
|
* $isTrue(true); //=> true |
|
15
|
|
|
* ``` |
|
16
|
|
|
* |
|
17
|
|
|
* @stream |
|
18
|
|
|
* @signature Boolean -> Boolean -> Boolean |
|
19
|
|
|
* @param bool $a |
|
|
|
|
|
|
20
|
|
|
* @param bool $b |
|
|
|
|
|
|
21
|
|
|
* @return bool |
|
22
|
|
|
*/ |
|
23
|
|
View Code Duplication |
function and_() { |
|
|
|
|
|
|
24
|
|
|
static $and = false; |
|
25
|
|
|
$and = $and ?: curry(function($a, $b){ |
|
26
|
|
|
return $a && $b; |
|
27
|
|
|
}); |
|
28
|
|
|
return _apply($and, func_get_args()); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Returns `$a || $b`. |
|
33
|
|
|
* |
|
34
|
|
|
* ```php |
|
35
|
|
|
* $isTrue = F\or_(false); |
|
36
|
|
|
* $isTrue(false); //=> false |
|
37
|
|
|
* $isTrue(true); //=> true |
|
38
|
|
|
* ``` |
|
39
|
|
|
* |
|
40
|
|
|
* @stream |
|
41
|
|
|
* @signature Boolean -> Boolean -> Boolean |
|
42
|
|
|
* @param bool $a |
|
|
|
|
|
|
43
|
|
|
* @param bool $b |
|
|
|
|
|
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
|
View Code Duplication |
function or_() { |
|
|
|
|
|
|
47
|
|
|
static $or = false; |
|
48
|
|
|
$or = $or ?: curry(function($a, $b){ |
|
49
|
|
|
return $a || $b; |
|
50
|
|
|
}); |
|
51
|
|
|
return _apply($or, func_get_args()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns `!$x`. |
|
56
|
|
|
* |
|
57
|
|
|
* ```php |
|
58
|
|
|
* F\map(F\not(), [true, false, true]); //=> [false, true, false] |
|
59
|
|
|
* ``` |
|
60
|
|
|
* |
|
61
|
|
|
* @stream |
|
62
|
|
|
* @signature Boolean -> Boolean |
|
63
|
|
|
* @param bool $x |
|
|
|
|
|
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
|
View Code Duplication |
function not() { |
|
|
|
|
|
|
67
|
|
|
static $not = false; |
|
68
|
|
|
$not = $not ?: curry(function($x) { |
|
69
|
|
|
return !$x; |
|
70
|
|
|
}); |
|
71
|
|
|
return _apply($not, func_get_args()); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Returns `$x == $y`. |
|
76
|
|
|
* |
|
77
|
|
|
* ```php |
|
78
|
|
|
* F\eq('10', 10); //=> true |
|
79
|
|
|
* ``` |
|
80
|
|
|
* |
|
81
|
|
|
* @stream |
|
82
|
|
|
* @signature * -> * -> Boolean |
|
83
|
|
|
* @param mixed $a |
|
|
|
|
|
|
84
|
|
|
* @param mixed $b |
|
|
|
|
|
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
|
|
function eq() { |
|
88
|
|
|
$eq = curry(function($a, $b){ |
|
89
|
|
|
return $a == $b; |
|
90
|
|
|
}); |
|
91
|
|
|
return _apply($eq, func_get_args()); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Returns `$x != $y`. |
|
96
|
|
|
* |
|
97
|
|
|
* ```php |
|
98
|
|
|
* F\notEq('Hi', 'Hello'); //=> true |
|
99
|
|
|
* ``` |
|
100
|
|
|
* |
|
101
|
|
|
* @stream |
|
102
|
|
|
* @signature * -> * -> Boolean |
|
103
|
|
|
* @param mixed $a |
|
|
|
|
|
|
104
|
|
|
* @param mixed $b |
|
|
|
|
|
|
105
|
|
|
* @return bool |
|
106
|
|
|
*/ |
|
107
|
|
View Code Duplication |
function notEq() { |
|
|
|
|
|
|
108
|
|
|
static $notEq = false; |
|
109
|
|
|
$notEq = $notEq ?: curry(function($a, $b){ |
|
110
|
|
|
return $a != $b; |
|
111
|
|
|
}); |
|
112
|
|
|
return _apply($notEq, func_get_args()); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Returns `$x === $y`. |
|
117
|
|
|
* |
|
118
|
|
|
* ```php |
|
119
|
|
|
* F\eqq(10, '10'); //=> false |
|
120
|
|
|
* ``` |
|
121
|
|
|
* @stream |
|
122
|
|
|
* @signature * -> * -> Boolean |
|
123
|
|
|
* @param mixed $a |
|
|
|
|
|
|
124
|
|
|
* @param mixed $b |
|
|
|
|
|
|
125
|
|
|
* @return bool |
|
126
|
|
|
*/ |
|
127
|
|
View Code Duplication |
function eqq() { |
|
|
|
|
|
|
128
|
|
|
static $eqq = false; |
|
129
|
|
|
$eqq = $eqq ?: curry(function($a, $b){ |
|
130
|
|
|
return $a === $b; |
|
131
|
|
|
}); |
|
132
|
|
|
return _apply($eqq, func_get_args()); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Returns `$x !== $y`. |
|
137
|
|
|
* |
|
138
|
|
|
* ```php |
|
139
|
|
|
* F\notEqq(10, '10'); //=> true |
|
140
|
|
|
* ``` |
|
141
|
|
|
* |
|
142
|
|
|
* @stream |
|
143
|
|
|
* @signature * -> * -> Boolean |
|
144
|
|
|
* @param mixed $a |
|
|
|
|
|
|
145
|
|
|
* @param mixed $b |
|
|
|
|
|
|
146
|
|
|
* @return bool |
|
147
|
|
|
*/ |
|
148
|
|
View Code Duplication |
function notEqq() { |
|
|
|
|
|
|
149
|
|
|
static $notEqq = false; |
|
150
|
|
|
$notEqq = $notEqq ?: curry(function($a, $b){ |
|
151
|
|
|
return $a !== $b; |
|
152
|
|
|
}); |
|
153
|
|
|
return _apply($notEqq, func_get_args()); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Returns `true` if the two elements have the same type and are deeply equivalent. |
|
158
|
|
|
* |
|
159
|
|
|
* ```php |
|
160
|
|
|
* $a = (object) ['a' => 1, 'b' => (object) ['c' => 'Hello'], 'd' => false]; |
|
161
|
|
|
* $b = (object) ['a' => 1, 'b' => (object) ['c' => 'Hi'], 'd' => false]; |
|
162
|
|
|
* $c = (object) ['a' => 1, 'b' => ['c' => 'Hello'], 'd' => false]; |
|
163
|
|
|
* // should have the same type |
|
164
|
|
|
* F\equals(5, '5'); //=> false |
|
165
|
|
|
* F\equals([1, 2, 3], [1, 2, 3]); //=> true |
|
166
|
|
|
* // should have the same order |
|
167
|
|
|
* F\equals([1, 3, 2], [1, 2, 3]); //=> false |
|
168
|
|
|
* F\equals($a, $b); //=> false |
|
169
|
|
|
* F\equals($a, $c); //=> false |
|
170
|
|
|
* $b->b->c = 'Hello'; |
|
171
|
|
|
* F\equals($a, $b); //=> true |
|
172
|
|
|
* ``` |
|
173
|
|
|
* |
|
174
|
|
|
* @stream |
|
175
|
|
|
* @signature * -> * -> Boolean |
|
176
|
|
|
* @param mixed $a |
|
|
|
|
|
|
177
|
|
|
* @param mixed $b |
|
|
|
|
|
|
178
|
|
|
* @return bool |
|
179
|
|
|
*/ |
|
180
|
|
|
function equals() { |
|
181
|
|
|
static $equals = false; |
|
182
|
|
|
$equals = $equals ?: curry(function($a, $b) { |
|
183
|
|
|
$type = type($a); |
|
184
|
|
|
if ($type != type($b)) |
|
185
|
|
|
return false; |
|
186
|
|
|
switch ($type) { |
|
187
|
|
|
case 'Null': |
|
188
|
|
|
case 'Boolean': |
|
189
|
|
|
case 'String': |
|
190
|
|
|
case 'Number': |
|
191
|
|
|
case 'Unknown': |
|
192
|
|
|
case 'Function': |
|
193
|
|
|
case 'Resource': |
|
194
|
|
|
case 'Error': |
|
195
|
|
|
case 'Stream': |
|
196
|
|
|
return $a == $b; |
|
197
|
|
|
case 'List': |
|
198
|
|
|
$length = length($a); |
|
199
|
|
|
return length($b) != $length ? false : |
|
200
|
|
|
0 == $length ? true : |
|
201
|
|
|
equals(head($a), head($b)) && equals(tail($a), tail($b)); |
|
202
|
|
|
case 'Array': |
|
203
|
|
|
case 'ArrayObject': |
|
204
|
|
|
case 'Object': |
|
205
|
|
|
return equals(keys($a), keys($b)) && equals(values($a), values($b)); |
|
206
|
|
|
} |
|
207
|
|
|
}); |
|
208
|
|
|
return _apply($equals, func_get_args()); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Returns `true` if the results of applying `$fn` to `$a` and `$b` are deeply equal. |
|
213
|
|
|
* |
|
214
|
|
|
* ```php |
|
215
|
|
|
* $headEquals = F\equalBy(F\head()); |
|
216
|
|
|
* $headEquals([1, 2], [1, 3]); //=> true |
|
217
|
|
|
* $headEquals([3, 2], [1, 3]); //=> false |
|
218
|
|
|
* |
|
219
|
|
|
* $sameAge = F\equalBy(F\get('age')); |
|
220
|
|
|
* $foo = ['name' => 'foo', 'age' => 11]; |
|
221
|
|
|
* $bar = ['name' => 'bar', 'age' => 13]; |
|
222
|
|
|
* $baz = ['name' => 'baz', 'age' => 11]; |
|
223
|
|
|
* $sameAge($foo, $bar); //=> false |
|
224
|
|
|
* $sameAge($foo, $baz); //=> true |
|
225
|
|
|
* ``` |
|
226
|
|
|
* |
|
227
|
|
|
* @stream |
|
228
|
|
|
* @signature (a -> b) -> a -> a -> Boolean |
|
229
|
|
|
* @return [type] [description] |
|
|
|
|
|
|
230
|
|
|
*/ |
|
231
|
|
View Code Duplication |
function equalBy() { |
|
|
|
|
|
|
232
|
|
|
static $equalBy = false; |
|
233
|
|
|
$equalBy = $equalBy ?: curry(function($fn, $a, $b) { |
|
234
|
|
|
return equals($fn($a), $fn($b)); |
|
235
|
|
|
}); |
|
236
|
|
|
return _apply($equalBy, func_get_args()); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Returns `$a < $b`. |
|
241
|
|
|
* |
|
242
|
|
|
* ```php |
|
243
|
|
|
* F\lt(3, 5); //=> true |
|
244
|
|
|
* F\lt(5, 5); //=> false |
|
245
|
|
|
* ``` |
|
246
|
|
|
* |
|
247
|
|
|
* @stream |
|
248
|
|
|
* @signature * -> * -> Boolean |
|
249
|
|
|
* @param mixed $a |
|
|
|
|
|
|
250
|
|
|
* @param mixed $b |
|
|
|
|
|
|
251
|
|
|
* @return bool |
|
252
|
|
|
*/ |
|
253
|
|
View Code Duplication |
function lt() { |
|
|
|
|
|
|
254
|
|
|
static $lt = false; |
|
255
|
|
|
$lt = $lt ?: curry(function($a, $b){ |
|
256
|
|
|
return $a < $b; |
|
257
|
|
|
}); |
|
258
|
|
|
return _apply($lt, func_get_args()); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Returns `$a <= $b`. |
|
263
|
|
|
* |
|
264
|
|
|
* ```php |
|
265
|
|
|
* F\lte(3, 5); //=> true |
|
266
|
|
|
* F\lte(5, 5); //=> true |
|
267
|
|
|
* ``` |
|
268
|
|
|
* |
|
269
|
|
|
* @stream |
|
270
|
|
|
* @signature * -> * -> Boolean |
|
271
|
|
|
* @param mixed $a |
|
|
|
|
|
|
272
|
|
|
* @param mixed $b |
|
|
|
|
|
|
273
|
|
|
* @return bool |
|
274
|
|
|
*/ |
|
275
|
|
View Code Duplication |
function lte() { |
|
|
|
|
|
|
276
|
|
|
static $lte = false; |
|
277
|
|
|
$lte = $lte ?: curry(function($a, $b){ |
|
278
|
|
|
return $a <= $b; |
|
279
|
|
|
}); |
|
280
|
|
|
return _apply($lte, func_get_args()); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Returns `$a > $b`. |
|
285
|
|
|
* |
|
286
|
|
|
* ```php |
|
287
|
|
|
* F\gt(5, 3); //=> true |
|
288
|
|
|
* F\gt(5, 5); //=> false |
|
289
|
|
|
* ``` |
|
290
|
|
|
* |
|
291
|
|
|
* @stream |
|
292
|
|
|
* @signature * -> * -> Boolean |
|
293
|
|
|
* @param mixed $a |
|
|
|
|
|
|
294
|
|
|
* @param mixed $b |
|
|
|
|
|
|
295
|
|
|
* @return bool |
|
296
|
|
|
*/ |
|
297
|
|
View Code Duplication |
function gt() { |
|
|
|
|
|
|
298
|
|
|
static $gt = false; |
|
299
|
|
|
$gt = $gt ?: curry(function($a, $b){ |
|
300
|
|
|
return $a > $b; |
|
301
|
|
|
}); |
|
302
|
|
|
return _apply($gt, func_get_args()); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* Returns `$a >= $b`. |
|
307
|
|
|
* |
|
308
|
|
|
* ```php |
|
309
|
|
|
* F\gte(5, 3); //=> true |
|
310
|
|
|
* F\gte(5, 5); //=> true |
|
311
|
|
|
* ``` |
|
312
|
|
|
* |
|
313
|
|
|
* @stream |
|
314
|
|
|
* @signature * -> * -> Boolean |
|
315
|
|
|
* @param mixed $a |
|
|
|
|
|
|
316
|
|
|
* @param mixed $b |
|
|
|
|
|
|
317
|
|
|
* @return bool |
|
318
|
|
|
*/ |
|
319
|
|
View Code Duplication |
function gte() { |
|
|
|
|
|
|
320
|
|
|
static $gte = false; |
|
321
|
|
|
$gte = $gte ?: curry(function($a, $b){ |
|
322
|
|
|
return $a >= $b; |
|
323
|
|
|
}); |
|
324
|
|
|
return _apply($gte, func_get_args()); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
|
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.