Skip to content

Instantly share code, notes, and snippets.

@bisby
Created May 21, 2015 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bisby/8946e5cd40a748d9873c to your computer and use it in GitHub Desktop.
Save bisby/8946e5cd40a748d9873c to your computer and use it in GitHub Desktop.
Geometric example
<?php
function sum($callback, $range) {
return array_reduce($range, function($carry, $item) use ($callback) {
return $carry + $callback($item);
});
}
$test = function($x) { return (float) 1/$x; };
$y = sum($test, range(1, 3)); // 1/1 + 1/2 + 1/3 = 11/6
if ($y !== (float) 11/6) { exit('Fail!'); }
$funct = function($x) { return (float) 1/(2**$x); };
$cap = isset($argv[1]) ? $argv[1] : 10;
echo sum($funct, range(1, $cap));
echo PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment