1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Dryist; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Count the number of items. |
9
|
|
|
* |
10
|
|
|
* @see https://php.net/iterator_count |
11
|
|
|
*/ |
12
|
|
|
function count(iterable $items): int |
13
|
|
|
{ |
14
|
6 |
|
$total = 0; |
15
|
6 |
|
foreach ($items as $_) { |
16
|
6 |
|
$total++; |
17
|
|
|
} |
18
|
6 |
|
return $total; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Combine a list of keys and a list of values into a map. |
23
|
|
|
*/ |
24
|
|
|
function combine(iterable $keys, iterable $values): iterable |
25
|
|
|
{ |
26
|
2 |
|
if (count($keys) !== count($values)) { |
27
|
1 |
|
throw new InvalidArgumentException("Count of keys and values do not match"); |
28
|
|
|
} |
29
|
|
|
|
30
|
1 |
|
foreach ($keys as $key) { |
31
|
1 |
|
yield $key => \current($values); |
32
|
1 |
|
\next($values); |
33
|
|
|
} |
34
|
1 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Resolve a list of keys from a map. |
38
|
|
|
*/ |
39
|
|
|
function keys(iterable $items): iterable |
40
|
|
|
{ |
41
|
4 |
|
foreach ($items as $key => $value) { |
42
|
4 |
|
yield $key; |
43
|
|
|
} |
44
|
4 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Apply a modifier to every item. |
48
|
|
|
* |
49
|
|
|
* Example modifier: |
50
|
|
|
* |
51
|
|
|
* function ($value) { |
52
|
|
|
* return $value; |
53
|
|
|
* } |
54
|
|
|
*/ |
55
|
|
|
function map(iterable $items, callable $modify): iterable |
56
|
|
|
{ |
57
|
4 |
|
foreach ($items as $key => $value) { |
58
|
4 |
|
yield $key => $modify($value); |
59
|
|
|
} |
60
|
4 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Apply a modifier to every item with the key. |
64
|
|
|
* |
65
|
|
|
* Example modifier: |
66
|
|
|
* |
67
|
|
|
* function ($key, $value) { |
68
|
|
|
* return $value; |
69
|
|
|
* } |
70
|
|
|
*/ |
71
|
|
|
function mapBoth(iterable $items, callable $modify): iterable |
72
|
|
|
{ |
73
|
3 |
|
foreach ($items as $key => $value) { |
74
|
3 |
|
yield $key => $modify($key, $value); |
75
|
|
|
} |
76
|
3 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Apply a modifier to every item key. |
80
|
|
|
* |
81
|
|
|
* Example modifier: |
82
|
|
|
* |
83
|
|
|
* function ($key) { |
84
|
|
|
* return $key; |
85
|
|
|
* } |
86
|
|
|
*/ |
87
|
|
|
function mapKey(iterable $items, callable $modify): iterable |
88
|
|
|
{ |
89
|
3 |
|
foreach ($items as $key => $value) { |
90
|
3 |
|
yield $modify($key) => $value; |
91
|
|
|
} |
92
|
3 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Resolve an iterable to an array. |
96
|
|
|
* |
97
|
|
|
* @link https://php.net/iterator_to_array |
98
|
|
|
*/ |
99
|
|
|
function resolve(iterable $items): array |
100
|
|
|
{ |
101
|
14 |
|
if (is_array($items)) { |
102
|
|
|
return $items; |
103
|
|
|
} |
104
|
|
|
|
105
|
14 |
|
return \iterator_to_array($items); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Limit items by a predicate applied to value. |
110
|
|
|
* |
111
|
|
|
* Example predicate: |
112
|
|
|
* |
113
|
|
|
* function ($value): bool { |
114
|
|
|
* return $value > 1; |
115
|
|
|
* } |
116
|
|
|
*/ |
117
|
|
|
function filter(iterable $items, callable $accept): iterable |
118
|
|
|
{ |
119
|
1 |
|
foreach ($items as $key => $item) { |
120
|
1 |
|
if ($accept($item)) { |
121
|
1 |
|
yield $key => $item; |
122
|
|
|
} |
123
|
|
|
} |
124
|
1 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Limit items by a predicate applied to key. |
128
|
|
|
* |
129
|
|
|
* Example predicate: |
130
|
|
|
* |
131
|
|
|
* function ($key): bool { |
132
|
|
|
* return $key % 2 === 0; |
133
|
|
|
* } |
134
|
|
|
*/ |
135
|
|
|
function filterKey(iterable $items, callable $accept): iterable |
136
|
|
|
{ |
137
|
1 |
|
foreach ($items as $key => $item) { |
138
|
1 |
|
if ($accept($key)) { |
139
|
1 |
|
yield $key => $item; |
140
|
|
|
} |
141
|
|
|
} |
142
|
1 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Limit items in a map by a list of keys. |
146
|
|
|
*/ |
147
|
|
|
function take(iterable $items, array $keys): iterable |
148
|
|
|
{ |
149
|
|
|
return filterKey($items, function ($key) use ($keys): bool { |
150
|
1 |
|
return \in_array($key, $keys, true); |
151
|
1 |
|
}); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Resolve a map into a list. |
156
|
|
|
*/ |
157
|
|
|
function values(iterable $items): iterable |
158
|
|
|
{ |
159
|
1 |
|
foreach ($items as $value) { |
160
|
1 |
|
yield $value; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|