-
Notifications
You must be signed in to change notification settings - Fork 16
/
a_lazy.php
65 lines (53 loc) · 1.52 KB
/
a_lazy.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
require_once __DIR__ . '/../autoload.php';
use const nspl\a\lazy\{take, map, filter};
use function nspl\a\lazy\with;
use function nspl\f\{pipe, partial, rpartial};
// Calls generator function and logs the yielded values
function logged(callable $generatorFunction)
{
static $count = 1;
return function(...$args) use ($generatorFunction, &$count) {
foreach ($generatorFunction(...$args) as $value) {
echo $count++ . '. ' . (string) $generatorFunction . ' -> ' . $value . "\n";
yield $value;
};
};
}
// Returns list of natural numbers starting 1 (never terminates)
function naturalNumbers()
{
$current = 1;
while (true) yield $current++;
}
const naturalNumbers = 'naturalNumbers';
// Returns square of a number
function square($n)
{
return $n * $n;
}
const square = 'square';
// Checks if a number is even
function isEven($n)
{
return $n % 2 === 0;
}
const isEven = 'isEven';
echo "Using f\pipe() function:\n\n";
$result = pipe(
logged(naturalNumbers)(), // from all natural numbers
partial(logged(filter), isEven), // filter only even numbers
rpartial(logged(take), 3), // take only first 3 even numbers
partial(logged(map), square) // and calculate their squares
);
foreach ($result as $value) {
echo "\nNext value is $value \n\n";
}
echo "The same solution with chaining:\n\n";
$result = with(logged(naturalNumbers)())
->filter(isEven)
->take(3)
->map(square);
foreach ($result as $value) {
echo "\nNext value is $value \n\n";
}