Changed first arg of lume.reduce to non-optional

This commit is contained in:
rxi 2014-02-28 00:26:35 +00:00
parent 736982c336
commit 50eb3d9f52
2 changed files with 7 additions and 9 deletions

View File

@ -80,12 +80,12 @@ calls to `fn` return true.
lume.all({1, 2, 1}, function(x) return x == 1 end) -- Returns 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 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` left to right, so as to reduce the array to a single value. The accumulator is
argument is not supplied the accumulator is initialised to `0`. intialised to the `first` value.
```lua ```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]) ### lume.set(t, [, retainkeys])

View File

@ -7,7 +7,7 @@
-- under the terms of the MIT license. See LICENSE for details. -- 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) function lume.clamp(x, min, max)
@ -103,10 +103,8 @@ end
function lume.reduce(t, fn, first) function lume.reduce(t, fn, first)
local acc = first for i = 1, #t do first = fn(first, t[i]) end
if acc == nil then acc = 0 end return first
for i = 1, #t do acc = fn(acc, t[i]) end
return acc
end end