1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Collections\Iterator; |
4
|
|
|
|
5
|
|
|
use Collections\ArrayList; |
6
|
|
|
use Collections\Dictionary; |
7
|
|
|
use Collections\Immutable\ImmArrayList; |
8
|
|
|
use Collections\Immutable\ImmDictionary; |
9
|
|
|
use Collections\Immutable\ImmSet; |
10
|
|
|
use Collections\Set; |
11
|
|
|
|
12
|
|
|
trait LazyKeyedIterableTrait |
13
|
|
|
{ |
14
|
|
|
public function toArray() |
15
|
|
|
{ |
16
|
|
|
$arr = []; |
17
|
|
|
foreach ($this as $k => $v) { |
|
|
|
|
18
|
|
|
$arr[$k] = $v; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
return $arr; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function toValuesArray() |
25
|
|
|
{ |
26
|
|
|
$arr = []; |
27
|
|
|
foreach ($this as $v) { |
|
|
|
|
28
|
|
|
$arr[] = $v; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $arr; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function toKeysArray() |
35
|
|
|
{ |
36
|
|
|
$arr = []; |
37
|
|
|
foreach ($this as $k => $_) { |
|
|
|
|
38
|
|
|
$arr[] = $k; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $arr; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function toVector() |
45
|
|
|
{ |
46
|
|
|
return new ArrayList($this); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function toImmVector() |
50
|
|
|
{ |
51
|
|
|
return new ImmArrayList($this); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function toMap() |
55
|
|
|
{ |
56
|
|
|
return new Dictionary($this); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function toImmMap() |
60
|
|
|
{ |
61
|
|
|
return new ImmDictionary($this); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function toSet() |
65
|
|
|
{ |
66
|
|
|
return new Set($this); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function toImmSet() |
70
|
|
|
{ |
71
|
|
|
return new ImmSet($this); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function lazy() |
75
|
|
|
{ |
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function values() |
80
|
|
|
{ |
81
|
|
|
return new LazyValuesIterable($this); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function keys() |
85
|
|
|
{ |
86
|
|
|
return new LazyKeysIterable($this); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function map($callback) |
90
|
|
|
{ |
91
|
|
|
return new LazyMapKeyedIterable($this, $callback); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function mapWithKey($callback) |
95
|
|
|
{ |
96
|
|
|
return new LazyMapWithKeyIterable($this, $callback); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function filter($callback) |
100
|
|
|
{ |
101
|
|
|
return new LazyFilterKeyedIterable($this, $callback); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function filterWithKey($callback) |
105
|
|
|
{ |
106
|
|
|
return new LazyFilterWithKeyIterable($this, $callback); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function zip($iterable) |
110
|
|
|
{ |
111
|
|
|
if (is_array($iterable)) { |
112
|
|
|
$iterable = new ImmDictionary($iterable); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return new LazyZipKeyedIterable($this, $iterable); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function take($n) |
119
|
|
|
{ |
120
|
|
|
return new LazyTakeKeyedIterable($this, $n); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function takeWhile($fn) |
124
|
|
|
{ |
125
|
|
|
return new LazyTakeWhileKeyedIterable($this, $fn); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function skip($n) |
129
|
|
|
{ |
130
|
|
|
return new LazySkipKeyedIterable($this, $n); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function skipWhile($fn) |
134
|
|
|
{ |
135
|
|
|
return new LazySkipWhileKeyedIterable($this, $fn); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function slice($start, $len) |
139
|
|
|
{ |
140
|
|
|
return new LazySliceKeyedIterable($this, $start, $len); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function concat($iterable) |
144
|
|
|
{ |
145
|
|
|
if (is_array($iterable)) { |
146
|
|
|
$iterable = new ImmDictionary($iterable); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return new LazyConcatIterable($this, $iterable); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function first() |
153
|
|
|
{ |
154
|
|
|
foreach ($this as $v) { |
|
|
|
|
155
|
|
|
return $v; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return null; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function firstKey() |
162
|
|
|
{ |
163
|
|
|
foreach ($this as $k => $_) { |
|
|
|
|
164
|
|
|
return $k; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return null; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function last() |
171
|
|
|
{ |
172
|
|
|
$v = null; |
173
|
|
|
foreach ($this as $v) { |
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $v; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function lastKey() |
180
|
|
|
{ |
181
|
|
|
$k = null; |
182
|
|
|
foreach ($this as $k => $_) { |
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $k; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function each(callable $callable) |
189
|
|
|
{ |
190
|
|
|
foreach ($this as $k => $v) { |
|
|
|
|
191
|
|
|
$callable($v, $k); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
} |