diff --git a/README.md b/README.md index f04cbc9..d2a63df 100644 --- a/README.md +++ b/README.md @@ -115,13 +115,6 @@ an error is raised, lume.reduce({1, 2, 3}, function(a, b) return a + b end) -- Returns 6 ``` -### lume.invert(t) -Returns a copy of the table where the keys have become the values and the -values the keys. -```lua -lume.invert({a = "x", b = "y"}) -- returns {x = "a", y = "b"} -``` - ### lume.set(t [, retainkeys]) Returns a copy of the `t` table with all the duplicate values removed. If `retainkeys` is true the table is not treated as an array and retains its @@ -160,6 +153,13 @@ than a string. Creates and returns a new array of the given slice. lume.slice({"a", "b", "c", "d", "e"}, 2, 4) -- Returns {"b", "c", "d"} ``` +### lume.invert(t) +Returns a copy of the table where the keys have become the values and the +values the keys. +```lua +lume.invert({a = "x", b = "y"}) -- returns {x = "a", y = "b"} +``` + ### lume.clone(t) Returns a shallow copy of the table `t`. diff --git a/lume.lua b/lume.lua index 6ad7a24..2d1c30c 100644 --- a/lume.lua +++ b/lume.lua @@ -76,6 +76,7 @@ function lume.shuffle(t) end + function lume.array(...) local t = {} for x in unpack({...}) do t[#t + 1] = x end @@ -126,13 +127,6 @@ function lume.reduce(t, fn, first) end -function lume.invert(t) - local rtn = {} - for k, v in pairs(t) do rtn[v] = k end - return rtn -end - - function lume.set(t, retainkeys) local rtn = {} for k, v in pairs(lume.invert(t)) do @@ -159,6 +153,7 @@ function lume.merge(t, t2, retainkeys) end + function lume.find(t, value) for k, v in pairs(t) do if v == value then return k end @@ -178,6 +173,13 @@ function lume.slice(t, i, j) end +function lume.invert(t) + local rtn = {} + for k, v in pairs(t) do rtn[v] = k end + return rtn +end + + function lume.clone(t) local rtn = {} for k, v in pairs(t) do rtn[k] = v end