Skip to content

Instantly share code, notes, and snippets.

@malisetti
Created September 10, 2015 16:26
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 malisetti/c06e46d3a0f35fdabe8a to your computer and use it in GitHub Desktop.
Save malisetti/c06e46d3a0f35fdabe8a to your computer and use it in GitHub Desktop.
writing code to sum things
<?php
function sum() {
$list = array();
$prepare = function($value) use(&$prepare, &$list) {
if (is_array($value)) {
return array_walk($value, $prepare, $list);
} else {
$list[] = $value;
}
};
$args = func_get_args();
array_walk($args, $prepare, $list);
return addition(0, $list);
}
function addition($sum, array &$list) {
return empty($list) ? $sum : addition($sum + array_pop($list), $list);
}
$vars = range(1, 1000);
echo sum($vars);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment