1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHP-UNDERSCORE package. |
5
|
|
|
* |
6
|
|
|
* (c) Jitendra Adhikari <[email protected]> |
7
|
|
|
* <https://github.com/adhocore> |
8
|
|
|
* |
9
|
|
|
* Licensed under MIT license. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ahc\Underscore; |
13
|
|
|
|
14
|
|
|
trait UnderscoreAliases |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Alias of first(). |
18
|
|
|
* |
19
|
|
|
* @param int $n |
20
|
|
|
* |
21
|
|
|
* @return array|mixed With n = 1 (default), it gives one item, which may not be array. |
22
|
|
|
*/ |
23
|
|
|
public function head($n = 1) |
24
|
|
|
{ |
25
|
|
|
return $this->first($n); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Alias of first(). |
30
|
|
|
* |
31
|
|
|
* @param int $n |
32
|
|
|
* |
33
|
|
|
* @return array|mixed With n = 1 (default), it gives one item, which may not be array. |
34
|
|
|
*/ |
35
|
|
|
public function take($n = 1) |
36
|
|
|
{ |
37
|
|
|
return $this->first($n); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Alias of last(). |
42
|
|
|
* |
43
|
|
|
* @param int $n |
44
|
|
|
* |
45
|
|
|
* @return array|mixed With n = 1 (default), it gives one item, which may not be array. |
46
|
|
|
*/ |
47
|
|
|
public function tail($n = 1) |
48
|
|
|
{ |
49
|
|
|
return $this->last($n); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Alias of last(). |
54
|
|
|
* |
55
|
|
|
* @param int $n |
56
|
|
|
* |
57
|
|
|
* @return array|mixed With n = 1 (default), it gives one item, which may not be array. |
58
|
|
|
*/ |
59
|
|
|
public function drop($n = 1) |
60
|
|
|
{ |
61
|
|
|
return $this->last($n); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Alias of unique(). |
66
|
|
|
* |
67
|
|
|
* @param callable|string $fn The callback. String is resolved to value of that index. |
68
|
|
|
* |
69
|
|
|
* @return self |
70
|
|
|
*/ |
71
|
|
|
public function uniq($fn = null) |
72
|
|
|
{ |
73
|
|
|
return $this->unique($fn); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Alias of difference(). |
78
|
|
|
* |
79
|
|
|
* @param array|mixed $data Array or array like or array convertible. |
80
|
|
|
* |
81
|
|
|
* @return self |
82
|
|
|
*/ |
83
|
|
|
public function without($data) |
84
|
|
|
{ |
85
|
|
|
return $this->difference($data); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Alias of map(). |
90
|
|
|
* |
91
|
|
|
* @param callable $fn The callback. |
92
|
|
|
* |
93
|
|
|
* @return self |
94
|
|
|
*/ |
95
|
|
|
public function collect(callable $fn) |
96
|
|
|
{ |
97
|
|
|
return $this->map($fn); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Alias of reduce(). |
102
|
|
|
* |
103
|
|
|
* @param callable $fn The callback. |
104
|
|
|
* @param mixed $memo The initial value carried over to each iteration and returned finally. |
105
|
|
|
* |
106
|
|
|
* @return mixed |
107
|
|
|
*/ |
108
|
|
|
public function foldl(callable $fn, $memo) |
109
|
|
|
{ |
110
|
|
|
return $this->reduce($fn, $memo); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Alias of reduce(). |
115
|
|
|
* |
116
|
|
|
* @param callable $fn The callback. |
117
|
|
|
* @param mixed $memo The initial value carried over to each iteration and returned finally. |
118
|
|
|
* |
119
|
|
|
* @return mixed |
120
|
|
|
*/ |
121
|
|
|
public function inject(callable $fn, $memo) |
122
|
|
|
{ |
123
|
|
|
return $this->reduce($fn, $memo); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Alias of reduceRight(). |
128
|
|
|
* |
129
|
|
|
* @param callable $fn The callback. |
130
|
|
|
* @param mixed $memo The initial value carried over to each iteration and returned finally. |
131
|
|
|
* |
132
|
|
|
* @return mixed |
133
|
|
|
*/ |
134
|
|
|
public function foldr(callable $fn, $memo) |
135
|
|
|
{ |
136
|
|
|
return $this->reduceRight($fn, $memo); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Alias of find(). |
141
|
|
|
* |
142
|
|
|
* @param callable $fn The truth test callback. |
143
|
|
|
* @param bool $useValue Whether to return value or the index on match. |
144
|
|
|
* |
145
|
|
|
* @return mixed|null |
146
|
|
|
*/ |
147
|
|
|
public function detect(callable $fn) |
148
|
|
|
{ |
149
|
|
|
return $this->find($fn); |
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Alias of filter(). |
154
|
|
|
* |
155
|
|
|
* @param callable|string|null $fn The truth test callback. |
156
|
|
|
* |
157
|
|
|
* @return self |
158
|
|
|
*/ |
159
|
|
|
public function select(callable $fn = null) |
160
|
|
|
{ |
161
|
|
|
return $this->filter($fn); |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Alias of every(). |
166
|
|
|
* |
167
|
|
|
* @param callable $fn The truth test callback. |
168
|
|
|
* |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
|
|
public function all(callable $fn) |
172
|
|
|
{ |
173
|
|
|
return $this->every($fn); |
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Alias of some(). |
178
|
|
|
* |
179
|
|
|
* @param callable $fn The truth test callback. |
180
|
|
|
* |
181
|
|
|
* @return bool |
182
|
|
|
*/ |
183
|
|
|
public function any(callable $fn) |
184
|
|
|
{ |
185
|
|
|
return $this->some($fn); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Alias of contains(). |
190
|
|
|
*/ |
191
|
|
|
public function includes($item) |
192
|
|
|
{ |
193
|
|
|
return $this->contains($item); |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Alias of count(). |
198
|
|
|
* |
199
|
|
|
* @return int |
200
|
|
|
*/ |
201
|
|
|
public function size() |
202
|
|
|
{ |
203
|
|
|
return $this->count(); |
|
|
|
|
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|