summaryrefslogtreecommitdiff
path: root/lib/util.php
diff options
context:
space:
mode:
authorBrion Vibber <brion@pobox.com>2009-12-15 13:53:19 -0800
committerBrion Vibber <brion@pobox.com>2009-12-16 09:27:48 -0500
commit0158f4f73db1c6090c09da8cc3cdcfb97af3883b (patch)
treed9595dd5285fe0341c93338721cb5216579a86f9 /lib/util.php
parent00fb5feff8f2552f63f1ddc7b1bef25ebd408507 (diff)
PHP 5.3 closure-based implementation of curry(); old implementation used as fallback for older PHP versions. Added unit tests to confirm they both work!
Diffstat (limited to 'lib/util.php')
-rw-r--r--lib/util.php30
1 files changed, 17 insertions, 13 deletions
diff --git a/lib/util.php b/lib/util.php
index 14d666503..d4afafb4c 100644
--- a/lib/util.php
+++ b/lib/util.php
@@ -523,19 +523,23 @@ function callback_helper($matches, $callback, $notice_id) {
return substr($matches[0],0,$left) . $result . substr($matches[0],$right);
}
-function curry($fn) {
- //TODO switch to a PHP 5.3 function closure based approach if PHP 5.3 is used
- $args = func_get_args();
- array_shift($args);
- $id = uniqid('_partial');
- $GLOBALS[$id] = array($fn, $args);
- return create_function('',
- '$args = func_get_args(); '.
- 'return call_user_func_array('.
- '$GLOBALS["'.$id.'"][0],'.
- 'array_merge('.
- '$args,'.
- '$GLOBALS["'.$id.'"][1]));');
+if (version_compare(PHP_VERSION, '5.3.0', 'ge')) {
+ // lambda implementation in a separate file; PHP 5.2 won't parse it.
+ require_once INSTALLDIR . "/lib/curry.php";
+} else {
+ function curry($fn) {
+ $args = func_get_args();
+ array_shift($args);
+ $id = uniqid('_partial');
+ $GLOBALS[$id] = array($fn, $args);
+ return create_function('',
+ '$args = func_get_args(); '.
+ 'return call_user_func_array('.
+ '$GLOBALS["'.$id.'"][0],'.
+ 'array_merge('.
+ '$args,'.
+ '$GLOBALS["'.$id.'"][1]));');
+ }
}
function common_linkify($url) {