diff --git a/lib/underscore.lua b/lib/underscore.lua index 7415190..dd7d537 100644 --- a/lib/underscore.lua +++ b/lib/underscore.lua @@ -104,6 +104,10 @@ function Underscore.funcs.reduce(list, memo, func) return memo end +function Underscore.funcs.sum(list) + return Underscore.funcs.reduce(list, 0, function(a, b) return a + b end) +end + function Underscore.funcs.detect(list, func) for i in Underscore.iter(list) do if func(i) then return i end diff --git a/spec/sum_spec.lua b/spec/sum_spec.lua new file mode 100644 index 0000000..4d4ba53 --- /dev/null +++ b/spec/sum_spec.lua @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe["_.sum"] = function() + it["should return the sum of all elements of the list"] = function() + input = { 1,2,3 } + result = _.sum(input) + expect(result).should_be(6) + end +end + +spec:report(true)