Created
July 11, 2014 14:25
-
-
Save bondarewicz/e6d42f9f4f6e3362b8ce to your computer and use it in GitHub Desktop.
PHP: currying
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //http://refectoryofajumblesale.wordpress.com/2013/06/04/currying-php/ | |
| $add = function($a, $b, $c) { | |
| return $a + $b + $c; | |
| }; | |
| $add2 = curry($add, 2); | |
| echo $add2(4, 5); | |
| function curry($fn, $arg, $obj = null) { | |
| return function() use ($fn, $arg, $obj) { | |
| $args = func_get_args(); | |
| array_unshift($args, $arg); | |
| if($obj) { | |
| $fn = array($obj, $fn); | |
| } | |
| return call_user_func_array($fn, $args); | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thx, I included this into a snippet of highorder funcs: https://gist.github.com/coderofsalvation/b011bb0f6789044b4da1