1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace loophp\collection\Operation; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Generator; |
9
|
|
|
use loophp\iterators\IterableIteratorAggregate; |
10
|
|
|
use Traversable; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @immutable |
14
|
|
|
* |
15
|
|
|
* @template TKey |
16
|
|
|
* @template T |
17
|
|
|
*/ |
18
|
|
|
final class Product extends AbstractOperation |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @template UKey |
22
|
|
|
* @template U |
23
|
|
|
*/ |
24
|
4 |
|
public function __invoke(): Closure |
25
|
|
|
{ |
26
|
4 |
|
return |
27
|
|
|
/** |
28
|
|
|
* @param iterable<UKey, U> ...$iterables |
29
|
|
|
* |
30
|
|
|
* @return Closure(iterable<TKey, T>): Generator<int, array<array-key, T|U>> |
31
|
|
|
*/ |
32
|
4 |
|
static function (iterable ...$iterables): Closure { |
33
|
|
|
/** @var Closure(iterable<TKey, T>): Generator<int, list<T|U>> $pipe */ |
34
|
4 |
|
$pipe = (new Pipe())()( |
35
|
4 |
|
( |
36
|
|
|
/** |
37
|
|
|
* @param array<int, Traversable<UKey, U>> $iterables |
|
|
|
|
38
|
|
|
*/ |
39
|
4 |
|
static fn (array $iterables): Closure => |
40
|
|
|
/** |
41
|
|
|
* @param iterable<TKey, T> $iterable |
42
|
|
|
*/ |
43
|
4 |
|
static fn (iterable $iterable): Generator => ( |
44
|
|
|
/** |
45
|
|
|
* @param Closure(iterable<TKey, T>): (Closure(iterable<UKey, U>): Generator<array<array-key, T|U>>) $f |
46
|
|
|
*/ |
47
|
4 |
|
static fn (Closure $f): Closure => (new FoldLeft())()( |
48
|
|
|
/** |
49
|
|
|
* @param iterable<UKey, U> $a |
50
|
|
|
* @param iterable<TKey, T> $x |
51
|
|
|
*/ |
52
|
4 |
|
static fn (iterable $a, iterable $x): Generator => $f($x)($a) |
53
|
4 |
|
) |
54
|
4 |
|
)( |
55
|
|
|
/** |
56
|
|
|
* @param (iterable<TKey, T>|iterable<UKey, U>) $xs |
|
|
|
|
57
|
|
|
*/ |
58
|
4 |
|
static fn (iterable $xs): Closure => |
59
|
|
|
/** |
60
|
|
|
* @param iterable<int, array<array-key, T>> $as |
61
|
|
|
*/ |
62
|
4 |
|
static fn (iterable $as): Generator => (new FlatMap())()( |
63
|
|
|
/** |
64
|
|
|
* @param array<int, T> $a |
65
|
|
|
*/ |
66
|
4 |
|
static fn (array $a): Generator => (new FlatMap())()( |
67
|
|
|
/** |
68
|
|
|
* @param T|U $x |
69
|
|
|
* |
70
|
|
|
* @return Generator<int, array<array-key, T|U>> |
71
|
|
|
*/ |
72
|
4 |
|
static fn (mixed $x): Generator => yield [...$a, $x] |
73
|
4 |
|
)($xs) |
74
|
4 |
|
)($as) |
75
|
4 |
|
)([[]])([$iterable, ...$iterables]) |
76
|
4 |
|
)( |
77
|
4 |
|
array_map( |
78
|
|
|
/** |
79
|
|
|
* @param iterable<UKey, U> $iterable |
80
|
|
|
* |
81
|
|
|
* @return IterableIteratorAggregate<UKey, U> |
82
|
|
|
*/ |
83
|
4 |
|
static fn (iterable $iterable): IterableIteratorAggregate => new IterableIteratorAggregate($iterable), |
84
|
4 |
|
$iterables |
85
|
4 |
|
) |
86
|
4 |
|
), |
87
|
4 |
|
((new Unwrap())()), |
88
|
4 |
|
((new Normalize())()) |
89
|
4 |
|
); |
90
|
|
|
|
91
|
|
|
// Point free style. |
92
|
4 |
|
return $pipe; |
93
|
4 |
|
}; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|