Created
July 11, 2015 21:28
-
-
Save slifin/d0655bc23e006130b958 to your computer and use it in GitHub Desktop.
PHP currying with placeholder support
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 | |
class Placeholder{} | |
function curry(callable $func, ... $curriedArgs) { | |
return function(...$fulfillment) use ($func, $curriedArgs) { | |
return $func(...array_merge(array_map(function($arg) use ($fulfillment) { | |
if ($arg instanceof Placeholder) | |
return array_shift($fulfillment); | |
return $arg; | |
}, $curriedArgs), $fulfillment)); | |
}; | |
}; | |
$sum = function ($a, $b, $c) { | |
var_dump($a,$b,$c); | |
return $a + $b + $c; | |
}; | |
$sum = curry($sum, 1,(new Placeholder),3); | |
$sum(2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment