-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo.php
159 lines (157 loc) · 4.23 KB
/
demo.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<html>
<head>
<title>PHPixing php</title>
</head>
<body>
<?php
/**
* Created by PhpStorm.
* User: rgladson
* Date: 12/30/2015
* Time: 10:33 AM
*/
require __DIR__ . '/vendor/autoload.php';
use PHPixme as P;
const BR = '<br/>' . PHP_EOL;
const testData = [1, 2, 4, 8, 16];
$jsonTestData = json_encode(testData);
$testFn = function (...$args) {
$output = implode(', ', $args);
echo '$testFn received: ' . $output . BR;
return $output;
};
$timesTwo = function ($value) {
return $value * 2;
};
$stringify = function ($value) {
return "'$value'";
};
$curry2 = P\curry(2);
$quaternary = P\nAry(4);
$map = P\binary('array_map');
$mapX2 = $map($timesTwo);
$joinComma = P\binary('implode')->__invoke(', ');
$makeView = P\combine($stringify, $timesTwo);
?>
<h1>Time to fix PHP!</h1>
<p>
Behold! There is nothing up my sleeves!<br>
<?= $jsonTestData ?>
</p>
<section>
<h2>Testing Quaternary!</h2>
<p>
<?php
$output = call_user_func_array($quaternary($testFn), testData)
?>
Quaternary returned: <?= $output ?>
</p>
</section>
<section>
<h2>Testing Unary</h2>
<p>
<?php
$output = call_user_func_array(P\unary($testFn), testData)
?>
Unary returned: <?= $output ?>
</p>
</section>
<section>
<h2>Currying native function test. Target: array_map</h2>
<p>
<?= $jsonTestData ?> —$map2x→ <?= json_encode($mapX2(testData)) ?>
</p>
</section>
<section>
<h2>imploding with a pre-formatted join!</h2>
<p>
<?= $jsonTestData ?> —$joinComma→ <?= $joinComma(testData) ?>
</p>
</section>
<section>
<h2>Combine $timesTwo with $stringify, then map it and join it through the pre-formated join!</h2>
<p>
<?= $jsonTestData ?> —map($makeView)→
<?= $joinComma($map($makeView, testData)) ?>
</p>
</section>
<section>
<h2>Again, but now using fold to produce the join!</h2>
<p>
<?= $jsonTestData ?> —map($makeView)—fold→
<?= P\fold(function ($output, $value) {
return $output ? "$output, $value" : $value;
}, '', $map($makeView, testData)) ?>
</p>
</section>
<section>
<h2>Again, but now using reduce to produce the join!</h2>
<p>
<?= $jsonTestData ?> —map($makeView)—reduce→
<?= P\reduce(function ($output, $value) {
return "$output, $value";
}, $map($makeView, testData)) ?>
</p>
</section>
<section>
<h2>Watch in <i>horror</i> as I flip a native function! array_filter</h2>
<p>
<?= $jsonTestData ?> —map2x—filterWith(_ mod 8 = 0)—values→
<?= json_encode(array_values(
P\flip('array_filter')
->__invoke(function ($value) {
return $value % 8 === 0;
})
->__invoke($mapX2(testData))
)) ?>
</p>
</section>
<section>
<h2>For my next trick, I will flip array_walk! (With a little help from binary)</h2>
<p>
The key-value pairs of <?= $jsonTestData ?>:
</p>
<ul>
<?php
$keyPairs = P\flip(
P\binary('array_walk')
)->__invoke(
function ($value, $key) {
echo "<li>$key ⇒ $value</li>" . PHP_EOL;
}
);
$keyPairs(testData);
?>
</ul>
</section>
<section>
<h2>Thrill as I convert this Hash Array to an array of Tupples!</h2>
<p>
<?php
$tupleMaker = P\S(
P\ternary('array_map')
->__invoke(
function ($val, $key) {
return [$key, $val];
}
),
'array_keys'
);
$testDataAsTuple = $tupleMaker(testData);
?>
<?= $jsonTestData ?> —$map(x, y -> [y,x])→
<?= json_encode($testDataAsTuple) ?>
</p>
<p>And to show why I picked these numbers</p>
<ul>
<?php
P\flip(
P\binary('array_walk')
)->__invoke(function ($tuple) {
echo "<li>2<sup>$tuple[0]</sup> = $tuple[1]</li>" . PHP_EOL;
}, $testDataAsTuple);
?>
</ul>
</section>
</body>
</html>