Created
September 10, 2015 16:26
-
-
Save malisetti/c06e46d3a0f35fdabe8a to your computer and use it in GitHub Desktop.
writing code to sum things
This file contains 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 | |
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