-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogic.php
More file actions
364 lines (346 loc) Β· 7.87 KB
/
Logic.php
File metadata and controls
364 lines (346 loc) Β· 7.87 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
namespace Vector\Lib;
use Vector\Core\Curry;
use Vector\Core\Module;
class Logic
{
use Module;
/**
* Logical Or Combinator
*
* Given n functions {f1, f2, ..., fn}, combine them in such a way to produce a new
* function g that returns true given at least one of {f1(x), f2(x), ... fn(x)} return true.
*
* @param array $fs array of functions to combine
* @param mixed $a value to test
* @return \Closure test for or using provided functions
* @example
* $funcF = function($x) { return $x >= 5; };
* $funcG = function($x) { return $x == 0; };
* $combinator = Logic::orCombinator([$funcF, $funcG]);
* $combinator(9); // True
* $combinator(0); // True
* $combinator(2); // False
*
* @type [(a -> Bool)] -> a -> Bool
*
*/
#[Curry]
protected static function orCombinator(array $fs, $a)
{
return self::using('any')(Arrays::map(function ($c) use ($a) {
return $c($a);
}, $fs));
}
/**
* Logical And Combinator
*
* Given n functions {f1, f2, ..., fn}, combine them in such a way to produce a new
* function g that returns true given {f1(x), f2(x), ... fn(x)} all return true.
*
* @param array $fs array of functions to combine
* @param mixed $a value to test
* @return \Closure test for or using provided functions
* @example
* $funcF = function($x) { return $x < 5; };
* $funcG = function($x) { return $x > 0; };
* $combinator = Logic::andCombinator([$funcF, $funcG]);
* $combinator(4); // True
* $combinator(2); // True
* $combinator(7); // False
*
* @type [(a -> Bool)] -> a -> Bool
*
*/
#[Curry]
protected static function andCombinator(array $fs, $a)
{
return self::all(Arrays::map(function ($c) use ($a) {
return $c($a);
}, $fs));
}
/**
* Logical Or
*
* Returns true given $a OR $b returns true.
*
* @param mixed $a First value
* @param mixed $b Second value
* @return Bool Result of OR
* @example
* Logic::logicalOr(false, false); // False
*
* @type Bool -> Bool -> Bool
*
* @example
* Logic::logicalOr(true, false); // True
*
*/
#[Curry]
protected static function logicalOr($a, $b)
{
return $a || $b;
}
/**
* Logical Not
*
* Returns true given $a is false.
* Returns false given $a is true.
*
* @param mixed $a value
* @return Bool Result of NOT
* @example
* Logic::logicalNot(true); // False
*
* @example
* Logic::logicalNot(false); // True
*
* @type Bool -> Bool
*
*/
#[Curry]
protected static function logicalNot($a)
{
return ! $a;
}
/**
* Logical And
*
* Returns true given $a AND $b are true.
*
* @param mixed $a First value
* @param mixed $b Second value
* @return Bool Result of AND
* @example
* Logic::logicalAnd(true, false); // False
*
* @type Bool -> Bool -> Bool
*
* @example
* Logic::logicalAnd(true, true); // True
*
*/
#[Curry]
protected static function logicalAnd($a, $b)
{
return $a && $b;
}
/**
* Greater Than
*
* Returns true given $b is greater than $a.
*
* @param mixed $a Value
* @param mixed $b Value to test
* @return Bool is $b greater than $a
* @example
* Logic::gt(1, 2); // True
*
* @type mixed -> mixed -> Bool
*
* @example
* Logic::gt(2, 1); // False
*
*/
#[Curry]
protected static function gt($a, $b)
{
return $b > $a;
}
/**
* Greater Than Or Equal
*
* Returns true given $b is greater than or equal to $a.
*
* @param mixed $a Value
* @param mixed $b Value to test
* @return Bool is $b greater than or equal to $a
* @example
* Logic::gte(1, 2); // True
*
* @type mixed -> mixed -> Bool
*
* @example
* Logic::gte(1, 1); // True
*
*/
#[Curry]
protected static function gte($a, $b)
{
return $b >= $a;
}
/**
* Less Than
*
* Returns true given $b is less than $a.
*
* @param mixed $a Value
* @param mixed $b Value to test
* @return Bool is $b less than $a
* @example
* Logic::lt(1, 2); // False
*
* @type mixed -> mixed -> Bool
*
* @example
* Logic::lt(2, 1); // True
*
*/
#[Curry]
protected static function lt($a, $b)
{
return $b < $a;
}
/**
* Less Than Or Equal
*
* Returns true given $b is less than or equal to $a.
*
* @param mixed $a Value
* @param mixed $b Value to test
* @return Bool is $b less than or equal to $a
* @example
* Logic::lte(2, 1); // True
*
* @type mixed -> mixed -> Bool
*
* @example
* Logic::lte(1, 1); // True
*
*/
#[Curry]
protected static function lte($a, $b)
{
return $b <= $a;
}
/**
* Equal (Not Strict / ==)
*
* Returns true given $a equals $b
*
* @param mixed $a First value
* @param mixed $b Second value
* @return Bool is $a equal to $b
* @example
* Logic::eq(1, 2); // False
*
* @type mixed -> mixed -> Bool
*
* @example
* Logic::eq(1, 1); // True
*
*/
#[Curry]
protected static function eq($a, $b)
{
return $a == $b;
}
/**
* Equal (Strict / ===)
*
* Returns true given $a equals $b
*
* @param mixed $a First value
* @param mixed $b Second value
* @return Bool is $a equal to $b
* @example
* Logic::eqStrict(1, '1'); // False
*
* @type mixed -> mixed -> Bool
*
* @example
* Logic::eqStrict(1, 1); // True
*
*/
#[Curry]
protected static function eqStrict($a, $b)
{
return $a === $b;
}
/**
* Not Equal (Not Strict / ==)
*
* Returns true given $a does not equal $b
*
* @param mixed $a First value
* @param mixed $b Second value
* @return Bool is $a not equal to $b
* @example
* Logic::notEq(1, 2); // True
*
* @type mixed -> mixed -> Bool
*
* @example
* Logic::notEq(1, 1); // False
*
*/
#[Curry]
protected static function notEq($a, $b)
{
return $a != $b;
}
/**
* Not Equal (Strict / ===)
*
* Returns true given $a does not equal $b
*
* @param mixed $a First value
* @param mixed $b Second value
* @return Bool is $a not equal to $b
* @example
* Logic::notEqStrict(1, '1'); // False
*
* @type mixed -> mixed -> Bool
*
* @example
* Logic::notEqStrict(1, 2); // True
*
*/
#[Curry]
protected static function notEqStrict($a, $b)
{
return $a !== $b;
}
/**
* All
*
* Returns true given all values are truthy
*
* @param array $arr Values to test
* @return Bool are all values truthy
* @example
* Logic::all(1, 'asdf', true); // True
*
* @example
* Logic::all(1, false); // False
*
* @type array -> Bool
*
*/
#[Curry]
protected static function all($arr)
{
return Arrays::reduce(self::using('logicalAnd'), true, $arr);
}
/**
* Any
*
* Returns true given any values are truthy
*
* @param array $arr Values to test
* @return Bool are any values truthy
* @example
* Logic::any(true, false); // True
*
* @example
* Logic::any(false, false); // False
*
* @type array -> Bool
*
*/
#[Curry]
protected static function any($arr)
{
return Arrays::reduce(self::using('logicalOr'), false, $arr);
}
}