Created
June 24, 2015 02:36
-
-
Save jimbocoder/fe720b4f70376d6bcef0 to your computer and use it in GitHub Desktop.
php function composition compose
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
function compose() { | |
$fxns = func_get_args(); | |
$outer = function($identity) { return $identity; }; | |
while($f = array_pop($fxns)) { | |
if ( !is_callable($f) ) { | |
throw new \Exception('This should be a better exception.'); | |
} | |
$outer = function() use($f, $outer) { | |
return $f(call_user_func_array($outer, func_get_args())); | |
}; | |
} | |
return $outer; | |
} | |
$password = 'PASSWORD'; | |
$rot13 = compose('str_rot13'); | |
// Hide our password from kid sister: | |
print $rot13($password) . "\n"; | |
$doubleRot13 = compose($rot13, 'str_rot13'); | |
// Whoops, overshot that one: | |
print $doubleRot13($password) . "\n"; | |
$uberHasher = compose('sha1', 'md5', 'crc32'); | |
// A REALLY SUPER ULTIMATE SECURE HASH: | |
print $uberHasher($password) . "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment