From 16f1c94e2add0fc54a1e689463f5f4da6866ed0f Mon Sep 17 00:00:00 2001 From: Evandro Leopoldino Gonalves Date: Wed, 23 Jul 2014 22:06:25 -0300 Subject: [PATCH 1/2] created sum method --- lib/underscore.lua | 4 ++++ 1 file changed, 4 insertions(+) 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 From 19010b1cac07bef4e759e33118842d8184ad61b8 Mon Sep 17 00:00:00 2001 From: Evandro Leopoldino Gonalves Date: Wed, 23 Jul 2014 22:06:46 -0300 Subject: [PATCH 2/2] created test to sum method --- spec/sum_spec.lua | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 spec/sum_spec.lua 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)