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