-
Notifications
You must be signed in to change notification settings - Fork 0
/
plumb.php
36 lines (29 loc) · 1.3 KB
/
plumb.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
<?php
// Grouping syntax
function __() { return array_merge(['grouped' => true], func_get_args()); }
call_user_func(function() {
$interpret = null;
$call = curry(function(array $env, $f, $arg) use (&$interpret) {
return op($f, $interpret($env, $arg));
});
$chain = curry(function(array $env, array $calls) use ($call) {
return array_reduce(
array_diff_key($calls, ['grouped' => '']),
$call($env),
function($x) { return $x; });
});
$plumb = curry(function(array $env, array $expr, $arg) use ($chain) {
return $expr? $chain(array_merge([$arg], $env), $expr)
: $arg;
});
$interpret = curry(function(array $e, $x) use ($chain, $plumb) {
// Assume ints and arrays are Plumb; anything else is PHP
if (is_int ($x)) return $e[$x];
if (is_array($x)) return isset($x['grouped'])
? $chain($e, $x)
: $plumb($e, $x);
return $x;
});
// Interpret $f in an empty environment
defun('plumb', $plumb([]));
});