Skip to content

Instantly share code, notes, and snippets.

@zackdouglas
Created July 11, 2010 22:17
Show Gist options
  • Save zackdouglas/471894 to your computer and use it in GitHub Desktop.
Save zackdouglas/471894 to your computer and use it in GitHub Desktop.
<?php
/**
* Currying in PHP 5.2.x for simple variables
* @author Zack Douglas <[email protected]>
*/
/**
* e.g.
* $arr = Array(1, 2, 3);
* $le2 = curry_left('ge', 2);
* $f_arr = array_filter($arr, $ge2);
* $f_arr == Array(1, 2);
* @param string-callback $fn
* @param simple $left_var
* @return mixed result of $fn($left_var, <some variable>)
*/
function curry_left($fn, $left_var) {
$args = '$v, $_=null';
$body = <<<body
\$curried_args = func_get_args();
array_unshift(\$curried_args, $left_var);
return call_user_func_array('$fn', \$curried_args);
body;
return create_function(
$args, $body);
}
/**
* e.g.
* $arr = Array(1, 2, 3);
* $le2 = curry_right('le', 2);
* $f_arr = array_filter($arr, $le2);
* $f_arr == Array(1, 2);
* @param string-callback $fn
* @param simple $right_var
* @return mixed result of $fn(<some variable>, $right_var)
*/
function curry_right($fn, $right_var) {
$args = '$v, $_=null';
$body = <<<body
\$curried_args = func_get_args();
array_push(\$curried_args, $right_var);
return call_user_func_array('$fn', \$curried_args);
body;
return create_function(
$args, $body);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment