Added isarray argument to lume.set

This commit is contained in:
rxi 2014-02-27 22:17:36 +00:00
parent 2636035974
commit eeadcd307d
2 changed files with 18 additions and 15 deletions

View File

@ -80,12 +80,6 @@ calls to `fn` return true.
lume.all({1, 2, 1}, function(x) return x == 1 end) -- Returns true
```
### lume.set(t)
Returns a copy of the array `t` with all the duplicate values removed.
```lua
lume.set({2, 1, 2, "cat", "cat"}) -- Returns {1, 2, cat}
```
### 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`
@ -94,6 +88,13 @@ argument is not supplied the accumulator is initialised to `0`.
lume.reduce({1, 2, 3}, function(a, b) return a + b end) -- Returns 6
```
### lume.set(t, [, isarray])
Returns a copy of the `t` table with all the duplicate values removed. If
`isarray` is true the table is treated as an array.
```lua
lume.set({2, 1, 2, "cat", "cat"}) -- Returns {1, 2, cat}
```
### lume.filter(t, fn [, isarray])
Iterates the table `t` and calls `fn` on each value. Returns a new table with
only the value which `fn` returned true. If `isarray` is true the table is

View File

@ -102,15 +102,6 @@ function lume.any(t, fn)
end
function lume.set(t)
local tmp = {}
for k, v in pairs(t) do tmp[v] = k end
local rtn = {}
for k, _ in pairs(tmp) do rtn[#rtn + 1] = k end
return rtn
end
function lume.reduce(t, fn, first)
local acc = first
if acc == nil then acc = 0 end
@ -119,6 +110,17 @@ function lume.reduce(t, fn, first)
end
function lume.set(t, isarray)
local tmp = {}
for k, v in pairs(t) do tmp[v] = k end
local rtn = {}
for k, v in pairs(tmp) do
rtn[isarray and (#rtn + 1) or v] = k
end
return rtn
end
function lume.filter(t, fn, isarray)
local rtn = {}
for k, v in pairs(t) do