1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Functions class. |
5
|
|
|
* @author Alexander Prokhorov |
6
|
|
|
* @license Simplified BSD |
7
|
|
|
* @link https://github.com/Athari/YaLinqo YaLinqo on GitHub |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace YaLinqo; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Container for standard functions in the form of closures. |
14
|
|
|
* @package YaLinqo |
15
|
|
|
*/ |
16
|
|
|
class Functions |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Identity function: returns the only argument. |
20
|
|
|
* @var callable {(x) ==> x} |
21
|
|
|
*/ |
22
|
|
|
public static $identity; |
23
|
|
|
/** |
24
|
|
|
* Key function: returns the second argument of two. |
25
|
|
|
* @var callable {(v, k) ==> k} |
26
|
|
|
*/ |
27
|
|
|
public static $key; |
28
|
|
|
/** |
29
|
|
|
* Value function: returns the first argument of two. |
30
|
|
|
* @var callable {(v, k) ==> v} |
31
|
|
|
*/ |
32
|
|
|
public static $value; |
33
|
|
|
/** |
34
|
|
|
* True function: returns true. |
35
|
|
|
* @var callable {() ==> true} |
36
|
|
|
*/ |
37
|
|
|
public static $true; |
38
|
|
|
/** |
39
|
|
|
* False function: returns false. |
40
|
|
|
* @var callable {() ==> false} |
41
|
|
|
*/ |
42
|
|
|
public static $false; |
43
|
|
|
/** |
44
|
|
|
* Blank function: does nothing. |
45
|
|
|
* @var callable {() ==> {}} |
46
|
|
|
*/ |
47
|
|
|
public static $blank; |
48
|
|
|
/** |
49
|
|
|
* Compare strict function: returns -1, 0 or 1 based on === and > operators. |
50
|
|
|
* @var callable |
51
|
|
|
*/ |
52
|
|
|
public static $compareStrict; |
53
|
|
|
/** |
54
|
|
|
* Compare strict function reversed: returns 1, 0 or -1 based on === and > operators. |
55
|
|
|
* @var callable |
56
|
|
|
*/ |
57
|
|
|
public static $compareStrictReversed; |
58
|
|
|
/** |
59
|
|
|
* Compare loose function: returns -1, 0 or 1 based on == and > operators. |
60
|
|
|
* @var callable |
61
|
|
|
*/ |
62
|
|
|
public static $compareLoose; |
63
|
|
|
/** |
64
|
|
|
* Compare loose function reversed: returns 1, 0 or -1 based on == and > operators. |
65
|
|
|
* @var callable |
66
|
|
|
*/ |
67
|
|
|
public static $compareLooseReversed; |
68
|
|
|
/** |
69
|
|
|
* Compare int function: returns the difference between the first and the second argument. |
70
|
|
|
* @var callable |
71
|
|
|
*/ |
72
|
|
|
public static $compareInt; |
73
|
|
|
/** |
74
|
|
|
* Compare int function reversed: returns the difference between the second and the first argument. |
75
|
|
|
* @var callable |
76
|
|
|
*/ |
77
|
|
|
public static $compareIntReversed; |
78
|
|
|
|
79
|
|
|
/** @internal */ |
80
|
|
|
public static function init() |
81
|
|
|
{ |
82
|
|
|
self::$identity = function($x) { return $x; }; |
83
|
|
|
|
84
|
|
|
/** @noinspection PhpUnusedParameterInspection */ |
85
|
|
|
self::$key = function($v, $k) { return $k; }; |
86
|
|
|
|
87
|
|
|
/** @noinspection PhpUnusedParameterInspection */ |
88
|
|
|
self::$value = function($v, $k) { return $v; }; |
89
|
|
|
|
90
|
|
|
self::$true = function() { return true; }; |
91
|
|
|
|
92
|
|
|
self::$false = function() { return false; }; |
93
|
|
|
|
94
|
|
|
self::$blank = function() { }; |
95
|
|
|
|
96
|
|
|
self::$compareStrict = function($a, $b) { |
97
|
|
|
if ($a === $b) |
98
|
|
|
return 0; |
99
|
|
|
elseif ($a > $b) |
100
|
|
|
return 1; |
101
|
|
|
else |
102
|
|
|
return -1; |
103
|
|
|
}; |
104
|
|
|
|
105
|
|
|
self::$compareStrictReversed = function($a, $b) { |
106
|
|
|
if ($a === $b) |
107
|
|
|
return 0; |
108
|
|
|
elseif ($a > $b) |
109
|
|
|
return -1; |
110
|
|
|
else |
111
|
|
|
return 1; |
112
|
|
|
}; |
113
|
|
|
|
114
|
|
|
self::$compareLoose = function($a, $b) { |
115
|
|
|
if ($a == $b) |
116
|
|
|
return 0; |
117
|
|
|
elseif ($a > $b) |
118
|
|
|
return 1; |
119
|
|
|
else |
120
|
|
|
return -1; |
121
|
|
|
}; |
122
|
|
|
|
123
|
|
|
self::$compareLooseReversed = function($a, $b) { |
124
|
|
|
if ($a == $b) |
125
|
|
|
return 0; |
126
|
|
|
elseif ($a > $b) |
127
|
|
|
return -1; |
128
|
|
|
else |
129
|
|
|
return 1; |
130
|
|
|
}; |
131
|
|
|
|
132
|
|
|
self::$compareInt = function($a, $b) { |
133
|
|
|
return $a - $b; |
134
|
|
|
}; |
135
|
|
|
|
136
|
|
|
self::$compareIntReversed = function($a, $b) { |
137
|
|
|
return $b - $a; |
138
|
|
|
}; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Increment function: returns incremental integers starting from 0. |
143
|
|
|
* @return callable |
144
|
|
|
*/ |
145
|
|
|
public static function increment() |
146
|
|
|
{ |
147
|
|
|
$i = 0; |
148
|
|
|
return function() use (&$i) { return $i++; }; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|