From 0158f4f73db1c6090c09da8cc3cdcfb97af3883b Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Tue, 15 Dec 2009 13:53:19 -0800 Subject: 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! --- lib/curry.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 lib/curry.php (limited to 'lib/curry.php') diff --git a/lib/curry.php b/lib/curry.php new file mode 100644 index 000000000..6136dcdc3 --- /dev/null +++ b/lib/curry.php @@ -0,0 +1,36 @@ +. + */ + +/** + * PHP 5.3 implementation of function currying, using native closures. + * On 5.2 and lower we use the fallback implementation in util.php + * + * @param callback $fn + * @param ... any remaining arguments will be appended to call-time params + * @return callback + */ +function curry($fn) { + $extra_args = func_get_args(); + array_shift($extra_args); + return function() use ($fn, $extra_args) { + $args = func_get_args(); + return call_user_func_array($fn, + array_merge($args, $extra_args)); + }; +} -- cgit v1.2.3-54-g00ecf