Created
April 21, 2013 23:40
-
-
Save ackintosh/5431584 to your computer and use it in GitHub Desktop.
Currying in PHP
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 | |
// before | |
function talk($person, $dialogue) | |
{ | |
echo "{$person} 「{$dialogue}」" . PHP_EOL; | |
} | |
talk('Bob', 'Hi'); | |
talk('Alice', 'Hi'); | |
/* | |
* Bob 「Hi」 | |
* Alice 「Hi」 | |
*/ | |
// currying | |
function talk($person) | |
{ | |
return function ($dialogue) use ($person) | |
{ | |
echo "{$person} 「{$dialogue}」" . PHP_EOL; | |
}; | |
} | |
$bob = talk('Bob'); | |
$alice = talk('Alice'); | |
$bob('Hi'); | |
$alice('Hi'); | |
/* | |
* Bob 「Hi」 | |
* Alice 「Hi」 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment