-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.php
47 lines (39 loc) · 1023 Bytes
/
map.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
namespace Idles;
function _map(?iterable $collection, ?callable $iteratee = null): iterable
{
$collection ??= [];
$iteratee ??= fn ($v) => $v;
if (\is_array($collection)) {
$res = [];
foreach ($collection as $k => $v) {
$res[$k] = $iteratee($v, $k, $collection);
}
return $res;
}
return new class ($collection, $iteratee) extends \IteratorIterator
{
public function __construct(\Iterator $it, callable $iteratee)
{
parent::__construct($it);
$this->iteratee = $iteratee;
}
#[\ReturnTypeWillChange]
public function current()
{
return ($this->iteratee)(parent::current(), $this->key(), $this);
}
private $iteratee;
};
}
function map(...$args)
{
return curryN(
2,
fn (callable $iteratee, ?iterable $collection) => _map($collection, $iteratee)
)(...$args);
}
function mapValues(...$args)
{
return map(...$args);
}