mirror of
https://github.com/TangentFoxy/lume.git
synced 2025-07-28 19:12:20 +00:00
Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
a4fe134985 | ||
|
cbff46bdfb | ||
|
5e15a57f37 | ||
|
2f388bc2c7 | ||
|
8f4d1b158f | ||
|
1bbb795a89 | ||
|
2c71079ef3 | ||
|
8a76fd7595 | ||
|
b4bea5f4e0 | ||
|
51189d190d |
17
README.md
17
README.md
@@ -105,12 +105,14 @@ supplied it is called on each value, true is returned if any of the calls to
|
|||||||
lume.any({1, 2, 1}, function(x) return x == 1 end) -- Returns true
|
lume.any({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. The accumulator is
|
left to right, so as to reduce the array to a single value. If a `first` value
|
||||||
intialised to the `first` value.
|
is specified the accumulator is initialised to this, otherwise the first value
|
||||||
|
in the array is used. If the array is empty and no `first` value is specified
|
||||||
|
an error is raised,
|
||||||
```lua
|
```lua
|
||||||
lume.reduce({1, 2, 3}, function(a, b) return a + b end, 0) -- Returns 6
|
lume.reduce({1, 2, 3}, function(a, b) return a + b end) -- Returns 6
|
||||||
```
|
```
|
||||||
|
|
||||||
### lume.set(t [, retainkeys])
|
### lume.set(t [, retainkeys])
|
||||||
@@ -151,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.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)
|
### lume.clone(t)
|
||||||
Returns a shallow copy of the table `t`.
|
Returns a shallow copy of the table `t`.
|
||||||
|
|
||||||
|
21
lume.lua
21
lume.lua
@@ -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.1.1" }
|
local lume = { _version = "1.1.2" }
|
||||||
|
|
||||||
|
|
||||||
function lume.clamp(x, min, max)
|
function lume.clamp(x, min, max)
|
||||||
@@ -119,16 +119,18 @@ end
|
|||||||
|
|
||||||
|
|
||||||
function lume.reduce(t, fn, first)
|
function lume.reduce(t, fn, first)
|
||||||
for i = 1, #t do first = fn(first, t[i]) end
|
local acc = first or t[1]
|
||||||
return first
|
assert(acc, "reduce of an empty array with no first value")
|
||||||
|
for i = first and 1 or 2, #t do acc = fn(acc, t[i]) end
|
||||||
|
return acc
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function lume.set(t, retainkeys)
|
function lume.set(t, retainkeys)
|
||||||
local tmp = {}
|
|
||||||
for k, v in pairs(t) do tmp[v] = k end
|
|
||||||
local rtn = {}
|
local rtn = {}
|
||||||
for k, v in pairs(tmp) do rtn[retainkeys and v or (#rtn + 1)] = k end
|
for k, v in pairs(lume.invert(t)) do
|
||||||
|
rtn[retainkeys and v or (#rtn + 1)] = k
|
||||||
|
end
|
||||||
return rtn
|
return rtn
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -169,6 +171,13 @@ function lume.slice(t, i, j)
|
|||||||
end
|
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)
|
function lume.clone(t)
|
||||||
local rtn = {}
|
local rtn = {}
|
||||||
for k, v in pairs(t) do rtn[k] = v end
|
for k, v in pairs(t) do rtn[k] = v end
|
||||||
|
@@ -150,12 +150,19 @@ end
|
|||||||
-- lume.reduce
|
-- lume.reduce
|
||||||
tests["lume.reduce"] = function()
|
tests["lume.reduce"] = function()
|
||||||
local concat = function(a, b) return a .. b end
|
local concat = function(a, b) return a .. b end
|
||||||
|
local add = function(a, b) return a + b end
|
||||||
testeq( lume.reduce({"cat", "dog"}, concat, ""), "catdog" )
|
testeq( lume.reduce({"cat", "dog"}, concat, ""), "catdog" )
|
||||||
testeq( lume.reduce({"cat", "dog"}, concat, "pig"), "pigcatdog" )
|
testeq( lume.reduce({"cat", "dog"}, concat, "pig"), "pigcatdog" )
|
||||||
|
testeq( lume.reduce({"me", "ow"}, concat), "meow" )
|
||||||
|
testeq( lume.reduce({1, 2, 3, 4}, add), 10 )
|
||||||
|
testeq( lume.reduce({1, 2, 3, 4}, add), 10 )
|
||||||
|
testeq( lume.reduce({1}, add), 1 )
|
||||||
|
testeq( lume.reduce({}, concat, "potato"), "potato" )
|
||||||
end
|
end
|
||||||
|
|
||||||
-- lume.set
|
-- lume.set
|
||||||
tests["lume.set"] = function()
|
tests["lume.set"] = function()
|
||||||
|
testeq( lume.set({}), {} )
|
||||||
local t = lume.set({1, 2, 3, 2, 5, 6, 6})
|
local t = lume.set({1, 2, 3, 2, 5, 6, 6})
|
||||||
table.sort(t)
|
table.sort(t)
|
||||||
testeq( t, {1, 2, 3, 5, 6} )
|
testeq( t, {1, 2, 3, 5, 6} )
|
||||||
@@ -196,6 +203,14 @@ tests["lume.slice"] = function()
|
|||||||
testeq( lume.slice({"a", "b", "c", "d", "e"}, 2, 1), {} )
|
testeq( lume.slice({"a", "b", "c", "d", "e"}, 2, 1), {} )
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- lume.invert
|
||||||
|
tests["lume_invert"] = function()
|
||||||
|
testeq( lume.invert({}), {} )
|
||||||
|
testeq( lume.invert{a = "x", b = "y"}, {x = "a", y = "b"} )
|
||||||
|
testeq( lume.invert{a = 1, b = 2}, {"a", "b"} )
|
||||||
|
testeq( lume.invert(lume.invert{a = 1, b = 2}), {a = 1, b = 2} )
|
||||||
|
end
|
||||||
|
|
||||||
-- lume.clone
|
-- lume.clone
|
||||||
tests["lume.clone"] = function()
|
tests["lume.clone"] = function()
|
||||||
local t = {6, 7, 4, 5}
|
local t = {6, 7, 4, 5}
|
||||||
|
Reference in New Issue
Block a user