diff --git a/README.md b/README.md index d831881..ad39d19 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lume.lua b/lume.lua index 9255c4e..ccf4f02 100644 --- a/lume.lua +++ b/lume.lua @@ -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