forked from finbarr/rubyphp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruby.php
274 lines (271 loc) · 6.12 KB
/
ruby.php
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
<?php
/*
* Author: Finbarr Taylor
* License: You can do whatever you want with this code.
*/
class Ruby {
/*
* returns TRUE iff $function evaluates TRUE for every member of $collection
*/
static function all($collection, $function)
{
foreach($collection as $item) {
if(!$function($item)) {
return FALSE;
}
}
return TRUE;
}
/*
* returns TRUE if $function evaluates TRUE for any member of $collection
*/
static function any($collection, $function)
{
foreach($collection as $item) {
if($function($item)) {
return TRUE;
}
}
return FALSE;
}
/*
* returns a new collection based on $collection with all NULL items removed
*/
static function compact($collection)
{
$result = array();
foreach($collection as $item) {
if($item != NULL) {
$result[] = $item;
}
}
return Ruby::wrap($result);
}
/*
* returns a new collection based on values returned by evaluating $function
* on each item in $collection
*/
static function collect($collection, $function)
{
return Ruby::wrap(array_map($function, $collection));
}
/*
* returns a new Ruby::Collection
*/
static function collection()
{
return new Collection();
}
/*
* evaluates $function on each item in $collection
*/
static function each($collection, $function)
{
foreach($collection as $item) {
$function($item);
}
return $collection;
}
/*
* evaluates $function on each index in $collection
*/
static function each_index($collection, $function)
{
foreach($collection as $index => $value) {
$function($index);
}
return $collection;
}
/*
* evaluates $function on each item and index in $collection
*/
static function each_with_index($collection, $function)
{
foreach($collection as $index => $item) {
$function($item, $index);
}
return $collection;
}
/*
* evaluates $function on each item in $collection and the last
* value returned from $function (initially the passed $variable)
*/
static function inject($collection, $variable, $function)
{
foreach($collection as $item) {
$variable = $function($variable, $item);
}
return $variable;
}
/*
* like Ruby::inject including the index of the item
*/
static function inject_with_index($collection, $variable, $function)
{
foreach($collection as $index => $item) {
$variable = $function($variable, $item, $index);
}
return $variable;
}
static function join($collection, $sep)
{
return implode($collection, $sep);
}
/*
* see Ruby::collect
*/
static function map($collection, $function)
{
return Ruby::collect($collection, $function);
}
/*
* see Ruby::inject
*/
static function reduce($collection, $variable, $function)
{
return Ruby::inject($collection, $variable, $function);
}
/*
* returns a new collection based on items in $collection for which
* $function evaluates to FALSE
*/
static function reject($collection, $function)
{
$result = array();
foreach($collection as $item) {
if(!$function($item)) {
$result[] = $item;
}
}
return Ruby::wrap($result);
}
/*
* returns a new collection based on items in $collection for which
* $function evaluates to TRUE
*/
static function select($collection, $function)
{
return Ruby::wrap(array_filter($collection, $function));
}
/*
* wraps an existing collection in a Ruby::Collection
*/
static function wrap($collection)
{
return new Collection($collection);
}
class Collection {
private $collection;
function __construct($existing = FALSE)
{
$this->collection = $existing ? $existing : array();
}
function all($function)
{
return Ruby::all($this->collection, $function);
}
function any($function)
{
return Ruby::any($this->collection, $function);
}
function clear()
{
unset($this->collection);
$this->collection = array();
return $this;
}
function collect($function)
{
return Ruby::collect($this->collection, $function);
}
function compact()
{
return Ruby::compact($this->collection);
}
function each($function)
{
Ruby::each($this->collection, $function);
return $this;
}
function each_index($function)
{
Ruby::each_index($this->collection, $function);
return $this;
}
function each_with_index($function)
{
Ruby::each_with_index($this->collection, $function);
return $this;
}
function empty()
{
return empty($this->collection);
}
function fetch($index)
{
return $this->collection[$index];
}
function inject($variable, $function)
{
return Ruby::inject($this->collection, $variable, $function);
}
function inject_with_index($variable, $function)
{
return Ruby::inject_with_index($this->collection, $variable, $function);
}
function join($sep)
{
return Ruby::join($this->collection, $sep);
}
function keys()
{
return Ruby::wrap(array_keys($this->collection));
}
function length()
{
return $this->size();
}
function map($function)
{
return $this->collect($function);
}
function push($variable)
{
$this->collection[] = $variable;
return $this;
}
function reduce($variable, $function)
{
return $this->inject($variable, $function);
}
function reject($function)
{
return Ruby::reject($this->collection, $function);
}
function select($function)
{
return Ruby::select($this->collection, $function);
}
function shift()
{
return array_shift($this->collection);
}
function size()
{
return count($this->collection);
}
function uniq()
{
return Ruby::wrap(array_unique($this->collection));
}
function unshift($variable)
{
array_unshift($this->collection, $variable);
return $this;
}
function unwrap()
{
return $this->collection;
}
}
}