|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Collections\Iterator; |
|
4
|
|
|
|
|
5
|
|
|
use Collections\ArrayList; |
|
6
|
|
|
use Collections\Immutable\ImmArrayList; |
|
7
|
|
|
use Collections\Immutable\ImmDictionary; |
|
8
|
|
|
use Collections\Immutable\ImmSet; |
|
9
|
|
|
use Collections\Set; |
|
10
|
|
|
|
|
11
|
|
|
trait LazyIterableTrait |
|
12
|
|
|
{ |
|
13
|
|
|
public function toArray() |
|
14
|
|
|
{ |
|
15
|
|
|
$arr = array(); |
|
16
|
|
|
foreach ($this as $v) { |
|
|
|
|
|
|
17
|
|
|
$arr[] = $v; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
return $arr; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function toValuesArray() |
|
24
|
|
|
{ |
|
25
|
|
|
return $this->toArray(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function toVector() |
|
29
|
|
|
{ |
|
30
|
|
|
return new ArrayList($this); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function toImmVector() |
|
34
|
|
|
{ |
|
35
|
|
|
return new ImmArrayList($this); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function toSet() |
|
39
|
|
|
{ |
|
40
|
|
|
return new Set($this); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function toImmSet() |
|
44
|
|
|
{ |
|
45
|
|
|
return new ImmSet($this); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function lazy() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function values() |
|
54
|
|
|
{ |
|
55
|
|
|
return new LazyValuesIterable($this); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function map(callable $callback) |
|
59
|
|
|
{ |
|
60
|
|
|
return new LazyMapIterable($this, $callback); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function filter(callable $callback) |
|
64
|
|
|
{ |
|
65
|
|
|
return new LazyFilterIterable($this, $callback); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function zip($iterable) |
|
69
|
|
|
{ |
|
70
|
|
|
if (is_array($iterable)) { |
|
71
|
|
|
$iterable = new ImmDictionary($iterable); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return new LazyZipIterable($this, $iterable); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function take($size = 1) |
|
78
|
|
|
{ |
|
79
|
|
|
return new LazyTakeIterable($this, $size); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function takeWhile($fn) |
|
83
|
|
|
{ |
|
84
|
|
|
return new LazyTakeWhileIterable($this, $fn); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function skip($n) |
|
88
|
|
|
{ |
|
89
|
|
|
return new LazySkipIterable($this, $n); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function skipWhile($fn) |
|
93
|
|
|
{ |
|
94
|
|
|
return new LazySkipWhileIterable($this, $fn); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function slice($start, $len) |
|
98
|
|
|
{ |
|
99
|
|
|
return new LazySliceIterable($this, $start, $len); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function concat($iterable) |
|
103
|
|
|
{ |
|
104
|
|
|
if (is_array($iterable)) { |
|
105
|
|
|
$iterable = new ImmDictionary($iterable); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return new LazyConcatIterable($this, $iterable); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function first() |
|
112
|
|
|
{ |
|
113
|
|
|
foreach ($this as $v) { |
|
|
|
|
|
|
114
|
|
|
return $v; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return null; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function last() |
|
121
|
|
|
{ |
|
122
|
|
|
$v = null; |
|
123
|
|
|
foreach ($this as $v) { |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $v; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function each(callable $callable) |
|
130
|
|
|
{ |
|
131
|
|
|
foreach ($this as $k => $v) { |
|
|
|
|
|
|
132
|
|
|
$callable($v, $k); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|