Skip to content

Instantly share code, notes, and snippets.

@CHH
Created December 6, 2010 13:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CHH/730287 to your computer and use it in GitHub Desktop.
Save CHH/730287 to your computer and use it in GitHub Desktop.
<?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