Created
June 12, 2012 15:55
-
-
Save kensnyder/2918358 to your computer and use it in GitHub Desktop.
Curry a function in PHP 5.3
This file contains hidden or 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 CurriedFunction { | |
public function __construct($callback/*[, $arg1][, $arg2][, $argN]*/) { | |
$this->args = func_get_args(); | |
$this->callback = array_shift($this->args); | |
} | |
public function __invoke(/*[, $arg1][, $arg2][, $argN]*/) { | |
$moreArgs = func_get_args(); | |
$args = array_merge($this->args, $moreArgs); | |
return call_user_func_array($this->callback, $args); | |
} | |
} | |
$world = new CurriedFunction('substr', 'Hello World.', 6); | |
echo $world(); // World. | |
echo '<br />'; | |
echo $world(1); // W |
Author
kensnyder
commented
Jun 12, 2012
via email
Ah cool. Thanks!
…On Tue, Jun 12, 2012 at 10:21 AM, John-David Dalton < ***@***.*** > wrote:
Technically it's not currying but partial application:
http://en.wikipedia.org/wiki/Currying#Contrast_with_partial_function_application
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/2918358
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment