mirror of
https://github.com/revarbat/BOSL2.git
synced 2025-08-13 21:54:31 +02:00
Speed improvement for sum() in vector and list of vectors case
This commit is contained in:
@@ -438,8 +438,11 @@ function lcm(a,b=[]) =
|
||||
// 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) =
|
||||
is_vector(v) ? [for(i=v) 1]*v :
|
||||
assert(is_consistent(v), "Input to sum is non-numeric or inconsistent")
|
||||
len(v) == 0 ? dflt : _sum(v,v[0]*0);
|
||||
is_vector(v[0]) ? [for(i=v) 1]*v :
|
||||
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);
|
||||
|
||||
|
Reference in New Issue
Block a user