added is_consistent, added error check to sum(), clarified docs to

is_path, and added fast versions of path2d, path3d and path4d.
This commit is contained in:
Adrian Mariano
2020-03-02 21:39:57 -05:00
parent af0e285781
commit 243dd7723e
4 changed files with 66 additions and 12 deletions

View File

@@ -432,9 +432,11 @@ function lcm(a,b=[]) =
// Example:
// sum([1,2,3]); // returns 6.
// sum([[1,2,3], [3,4,5], [5,6,7]]); // returns [9, 12, 15]
function sum(v, dflt=0, _i=0, _acc) =
_i>=len(v)? (len(v)? _acc : dflt) :
sum(v, dflt=dflt, _i=_i+1, _acc=is_undef(_acc)? v[_i] : _acc+v[_i]);
function sum(v, dflt=0) =
assert(is_consistent(v), "Input to sum is non-numeric or inconsistent")
len(v) == 0 ? dflt : _sum(v,v[0]*0);
function _sum(v,_total,_i=0) = _i>=len(v) ? _total : _sum(v,_total+v[_i], _i+1);
// Function: cumsum()