Skip to content

Instantly share code, notes, and snippets.

@ariela
Created April 23, 2014 08:59
Show Gist options
  • Save ariela/11207862 to your computer and use it in GitHub Desktop.
Save ariela/11207862 to your computer and use it in GitHub Desktop.
PHPでカリー化の実験
<?php
/**
* 指定値を最大値として、入力された値が最大値以上の場合は
* 最大値として扱うカリー化関数
* @param integer $max 最大値
* @return Closure 比較関数 f(x)
*/
function compare($max) {
return function ($x) use ($max) {
return ($x < $max) ? $x : $max;
};
}
// 最大値指定
$f = compare(10);
// 0は10以下なので「0」と表示
echo $f(0) . PHP_EOL;
// 5は10以下なので「5」と表示
echo $f(5) . PHP_EOL;
// 10は10以下なので「10」と表示
echo $f(10) . PHP_EOL;
// 99は10より大きいので「10」と表示
echo $f(99) . PHP_EOL;
// 9999は10より大きいので「10」と表示
echo $f(9999) . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment