From 50eb3d9f527d73c9acc911ed35884d6d22f98846 Mon Sep 17 00:00:00 2001 From: rxi Date: Fri, 28 Feb 2014 00:26:35 +0000 Subject: [PATCH] Changed `first` arg of lume.reduce to non-optional --- README.md | 8 ++++---- lume.lua | 8 +++----- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 698d359..0a5cd8e 100644 --- a/README.md +++ b/README.md @@ -80,12 +80,12 @@ calls to `fn` return true. lume.all({1, 2, 1}, function(x) return x == 1 end) -- Returns true ``` -### lume.reduce(t, fn [, first]) +### lume.reduce(t, fn, first) Applies `fn` on two arguments cumulative to the items of the array `t`, from -left to right, so as to reduce the array to a single value. If the `first` -argument is not supplied the accumulator is initialised to `0`. +left to right, so as to reduce the array to a single value. The accumulator is +intialised to the `first` value. ```lua -lume.reduce({1, 2, 3}, function(a, b) return a + b end) -- Returns 6 +lume.reduce({1, 2, 3}, function(a, b) return a + b end, 0) -- Returns 6 ``` ### lume.set(t, [, retainkeys]) diff --git a/lume.lua b/lume.lua index 9e35d43..2903d47 100644 --- a/lume.lua +++ b/lume.lua @@ -7,7 +7,7 @@ -- under the terms of the MIT license. See LICENSE for details. -- -local lume = { _version = "1.0.2" } +local lume = { _version = "1.0.3" } function lume.clamp(x, min, max) @@ -103,10 +103,8 @@ end function lume.reduce(t, fn, first) - local acc = first - if acc == nil then acc = 0 end - for i = 1, #t do acc = fn(acc, t[i]) end - return acc + for i = 1, #t do first = fn(first, t[i]) end + return first end