Skip to content

Instantly share code, notes, and snippets.

@slifin
Created July 11, 2015 21:28
Show Gist options
  • Save slifin/d0655bc23e006130b958 to your computer and use it in GitHub Desktop.
Save slifin/d0655bc23e006130b958 to your computer and use it in GitHub Desktop.
PHP currying with placeholder support
<?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