Skip to content

Instantly share code, notes, and snippets.

@katzchang
Created March 14, 2014 06:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katzchang/9542924 to your computer and use it in GitHub Desktop.
Save katzchang/9542924 to your computer and use it in GitHub Desktop.
<?PHP
class F {
public static function curry($f) {
return new F($f);
}
private function __construct($f, $args = []) {
$this->f = $f;
$this->args = $args;
}
public function _($arg = null) {
if ($arg === null) {
$f = $this->f;
return $f($this->args);
}
$args2 = $this->args;
$args2[] = $arg;
return new F($this->f, $args2);
}
}
$f = F::curry(function($_) {
return $_[0] + $_[1];
});
echo "result:\n";
echo $f->_(2)->_(3)->_() . "\n";
echo $f->_(3)->_(5)->_() . "\n";
$add2 = $f->_(2);
echo $add2->_(3)->_() . "\n";
@k-ohtani-is-deleting
Copy link

<?PHP
echo $f->_(1)->_(2)->_(3)->_();

@k-ohtani-is-deleting
Copy link

<?PHP
$fib = function($x) {
  return $x < 2
    ? $x
    : $fib($x-1) + $fib($x-2);
  })) // FAIL!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment