Skip to content

Instantly share code, notes, and snippets.

@ackintosh
Created April 21, 2013 23:40
Show Gist options
  • Save ackintosh/5431584 to your computer and use it in GitHub Desktop.
Save ackintosh/5431584 to your computer and use it in GitHub Desktop.
Currying in PHP
<?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