Created
December 6, 2010 13:27
-
-
Save CHH/730287 to your computer and use it in GitHub Desktop.
This file contains 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 | |
// pipes each function's return value as input into the next function in the chain | |
function func_compose() | |
{ | |
$fns = func_get_args(); | |
$composition = function() use ($fns) { | |
$input = func_get_args(); | |
foreach ($fns as $fn) { | |
$return = call_user_func_array($fn, $input); | |
$input = array($return); | |
} | |
return $return; | |
}; | |
return $composition; | |
} | |
$greet = function($name) { | |
return "Hello $name"; | |
}; | |
$exclaim = function($statement) { | |
return $statement . '!'; | |
}; | |
$fn = func_compose($greet, $exclaim); | |
echo $fn("World"); // $exclaim($greet("World")) -> Hello World! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment