-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lambda.php
56 lines (48 loc) · 1.18 KB
/
Lambda.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
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
/**
*
* The Lambda Class
*
* This class is taken directly from the Functional PHP Extension Library.
*
* @package MetaPartial Class
* @category Libraries
* @author Mathew Wong aka Jamongkad
*
*/
class Lambda {
private $args ,$expr;
function __construct($args = false,$expr = false) {
$this->args = $args;
$this->expr = $expr;
}
// Several places we need to make up unique function names.
// gensym() formalizes the process and makes it reusable.
function gensym() {
static $counter;
$counter ++;
return "__lambda_$counter";
}
//This has been slightly changed to NOT suppress errors
function def_fun($name, $args, $code) {
eval( "function $name ($args) { $code }" );
if (!function_exists($name)) die(
"<pre>Failed to define function:
\n\nfunction $name ($args) {\n$code\n}\n\n</pre>\n\n"
);
}
function evaluate() {
$name = $this->gensym();
$this->def_fun($name, $this->args, "return $this->expr;");
return $name;
}
}
function lam() {
$args = func_get_args();
$p = new Lambda($args[0],$args[1]);
return $p->evaluate();
}
function fx($body) {
return lam('$x', $body);
}
?>