1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Collections\Iterator; |
4
|
|
|
|
5
|
|
|
use Collections\Immutable\ImmArrayList; |
6
|
|
|
use Collections\Immutable\ImmDictionary; |
7
|
|
|
use Collections\Immutable\ImmSet; |
8
|
|
|
use Collections\Map; |
9
|
|
|
use Collections\Pair; |
10
|
|
|
use Collections\Set; |
11
|
|
|
use Collections\Vector; |
12
|
|
|
use Collections\VectorInterface; |
13
|
|
|
|
14
|
|
|
trait LazyIterableTrait |
15
|
|
|
{ |
16
|
|
|
public function toArray() |
17
|
|
|
{ |
18
|
|
|
$arr = array(); |
19
|
|
|
foreach ($this as $v) { |
|
|
|
|
20
|
|
|
$arr[] = $v; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
return $arr; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function toValuesArray() |
27
|
|
|
{ |
28
|
|
|
return $this->toArray(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function toVector() |
32
|
|
|
{ |
33
|
|
|
return new Vector($this); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function toImmVector() |
37
|
|
|
{ |
38
|
|
|
return new ImmArrayList($this); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function toSet() |
42
|
|
|
{ |
43
|
|
|
return new Set($this); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function toImmSet() |
47
|
|
|
{ |
48
|
|
|
return new ImmSet($this); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function lazy() |
52
|
|
|
{ |
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function values() |
57
|
|
|
{ |
58
|
|
|
return new LazyValuesIterable($this); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function map(callable $callback) |
62
|
|
|
{ |
63
|
|
|
return new LazyMapIterable($this, $callback); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function filter(callable $callback) |
67
|
|
|
{ |
68
|
|
|
return new LazyFilterIterable($this, $callback); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function zip($iterable) |
72
|
|
|
{ |
73
|
|
|
if (is_array($iterable)) { |
74
|
|
|
$iterable = new ImmDictionary($iterable); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return new LazyZipIterable($this, $iterable); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function take($size = 1) |
81
|
|
|
{ |
82
|
|
|
return new LazyTakeIterable($this, $size); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function takeWhile($fn) |
86
|
|
|
{ |
87
|
|
|
return new LazyTakeWhileIterable($this, $fn); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function skip($n) |
91
|
|
|
{ |
92
|
|
|
return new LazySkipIterable($this, $n); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function skipWhile($fn) |
96
|
|
|
{ |
97
|
|
|
return new LazySkipWhileIterable($this, $fn); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function slice($start, $len) |
101
|
|
|
{ |
102
|
|
|
return new LazySliceIterable($this, $start, $len); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function concat($iterable) |
106
|
|
|
{ |
107
|
|
|
if (is_array($iterable)) { |
108
|
|
|
$iterable = new ImmDictionary($iterable); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return new LazyConcatIterable($this, $iterable); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function first() |
115
|
|
|
{ |
116
|
|
|
foreach ($this as $v) { |
|
|
|
|
117
|
|
|
return $v; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return null; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function last() |
124
|
|
|
{ |
125
|
|
|
$v = null; |
126
|
|
|
foreach ($this as $v) { |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $v; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritDoc} |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
|
|
public function each(callable $callable) |
137
|
|
|
{ |
138
|
|
|
foreach ($this as $v) { |
|
|
|
|
139
|
|
|
$callable($v); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
|
|
public function exists(callable $fn) |
149
|
|
|
{ |
150
|
|
|
foreach ($this as $element) { |
|
|
|
|
151
|
|
|
if ($fn($element)) { |
152
|
|
|
return true; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return false; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
View Code Duplication |
public function concatAll() |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
/** @var VectorInterface $results */ |
162
|
|
|
$results = new static(); |
163
|
|
|
$this->each(function ($subArray) use ($results) { |
164
|
|
|
foreach ($subArray as $item) { |
165
|
|
|
$results->add($item); |
166
|
|
|
} |
167
|
|
|
}); |
168
|
|
|
|
169
|
|
|
return $results; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritDoc} |
174
|
|
|
* @return $this |
175
|
|
|
*/ |
176
|
|
View Code Duplication |
public function groupBy($callback) |
|
|
|
|
177
|
|
|
{ |
178
|
|
|
$group = new Map(); |
179
|
|
|
foreach ($this as $value) { |
|
|
|
|
180
|
|
|
$key = $callback($value); |
181
|
|
|
if (!$group->containsKey($key)) { |
182
|
|
|
$element = $this instanceof VectorInterface ? new static([$value]) : new Vector([$value]); |
|
|
|
|
183
|
|
|
$group->add(new Pair($key, $element)); |
184
|
|
|
} else { |
185
|
|
|
$value = $group->get($key)->add($value); |
186
|
|
|
$group->set($key, $value); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $group; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* {@inheritDoc} |
195
|
|
|
* @return $this |
196
|
|
|
*/ |
197
|
|
View Code Duplication |
public function indexBy($callback) |
|
|
|
|
198
|
|
|
{ |
199
|
|
|
$group = new Map(); |
200
|
|
|
foreach ($this as $value) { |
|
|
|
|
201
|
|
|
$key = $callback($value); |
202
|
|
|
$group->set($key, $value); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return $group; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* {@inheritdoc} |
210
|
|
|
*/ |
211
|
|
|
public function reduce(callable $callback, $initial = null) |
212
|
|
|
{ |
213
|
|
|
foreach ($this as $element) { |
|
|
|
|
214
|
|
|
$initial = $callback($initial, $element); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $initial; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|