mirror of
https://github.com/TangentFoxy/lume.git
synced 2025-07-28 11:02:20 +00:00
Compare commits
15 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
a4fe134985 | ||
|
cbff46bdfb | ||
|
5e15a57f37 | ||
|
2f388bc2c7 | ||
|
8f4d1b158f | ||
|
1bbb795a89 | ||
|
2c71079ef3 | ||
|
8a76fd7595 | ||
|
b4bea5f4e0 | ||
|
51189d190d | ||
|
e6d47627cd | ||
|
05828bd840 | ||
|
83a051aadb | ||
|
2699094218 | ||
|
a08436445d |
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.reduce(t, fn, first)
|
||||
### 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. The accumulator is
|
||||
intialised to the `first` value.
|
||||
left to right, so as to reduce the array to a single value. If a `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
|
||||
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])
|
||||
@@ -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.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`.
|
||||
|
||||
|
30
lume.lua
30
lume.lua
@@ -7,11 +7,11 @@
|
||||
-- under the terms of the MIT license. See LICENSE for details.
|
||||
--
|
||||
|
||||
local lume = { _version = "1.1.0" }
|
||||
local lume = { _version = "1.1.2" }
|
||||
|
||||
|
||||
function lume.clamp(x, min, max)
|
||||
return math.max(math.min(x, max), min)
|
||||
return x < min and min or (x > max and max or x)
|
||||
end
|
||||
|
||||
|
||||
@@ -43,7 +43,9 @@ end
|
||||
|
||||
|
||||
function lume.distance(x1, y1, x2, y2, squared)
|
||||
local s = (x1 - x2) ^ 2 + (y1 - y2) ^ 2
|
||||
local dx = x1 - x2
|
||||
local dy = y1 - y2
|
||||
local s = dx * dx + dy * dy
|
||||
return squared and s or math.sqrt(s)
|
||||
end
|
||||
|
||||
@@ -117,16 +119,18 @@ end
|
||||
|
||||
|
||||
function lume.reduce(t, fn, first)
|
||||
for i = 1, #t do first = fn(first, t[i]) end
|
||||
return first
|
||||
local acc = first or t[1]
|
||||
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
|
||||
|
||||
|
||||
function lume.set(t, retainkeys)
|
||||
local tmp = {}
|
||||
for k, v in pairs(t) do tmp[v] = k end
|
||||
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
|
||||
end
|
||||
|
||||
@@ -167,6 +171,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
|
||||
@@ -264,12 +275,13 @@ function lume.hotswap(modname)
|
||||
if type(v) == "table" then update(old[k], v) else old[k] = v end
|
||||
end
|
||||
end
|
||||
local oldmod = pcall(require, modname) or nil
|
||||
local err = nil
|
||||
local function onerror(e)
|
||||
for k, v in pairs(_G) do _G[k] = oldglobal[k] end
|
||||
err = lume.trim(e)
|
||||
end
|
||||
local ok, oldmod = pcall(require, modname)
|
||||
oldmod = ok and oldmod or nil
|
||||
xpcall(function()
|
||||
package.loaded[modname] = nil
|
||||
local newmod = require(modname)
|
||||
|
@@ -64,11 +64,11 @@ end
|
||||
|
||||
-- lume.distance
|
||||
tests["lume.distance"] = function()
|
||||
testeq( lume.distance(10, 20, 10, 20), 0 )
|
||||
testeq( lume.distance(10, 20, 20, 20), 10 )
|
||||
local x = lume.distance(1, 2, 5, 7)
|
||||
testeq( lume.distance(1, 2, 5, 7, true), x * x )
|
||||
testeq( lume.distance(10, 10, 10, 20, true), 10 * 10 )
|
||||
testeq( lume.distance(15, 20, 15, 20), 0 )
|
||||
testeq( lume.distance(13, 44, 156, 232), 236.205419074 )
|
||||
testeq( lume.distance(-23, 66, -232, 123), 216.633330769 )
|
||||
local x = lume.distance(13, 15, -2, 81)
|
||||
testeq( lume.distance(13, 15, -2, 81, true), x * x )
|
||||
end
|
||||
|
||||
-- lume.angle
|
||||
@@ -103,7 +103,6 @@ tests["lume.shuffle"] = function()
|
||||
testeq( lume.shuffle({}), {} )
|
||||
end
|
||||
|
||||
|
||||
-- lume.array
|
||||
tests["lume.array"] = function()
|
||||
local t = lume.array(pairs({a=0, b=0, c=0}))
|
||||
@@ -151,12 +150,19 @@ end
|
||||
-- lume.reduce
|
||||
tests["lume.reduce"] = function()
|
||||
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, "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
|
||||
|
||||
-- lume.set
|
||||
tests["lume.set"] = function()
|
||||
testeq( lume.set({}), {} )
|
||||
local t = lume.set({1, 2, 3, 2, 5, 6, 6})
|
||||
table.sort(t)
|
||||
testeq( t, {1, 2, 3, 5, 6} )
|
||||
@@ -197,6 +203,14 @@ tests["lume.slice"] = function()
|
||||
testeq( lume.slice({"a", "b", "c", "d", "e"}, 2, 1), {} )
|
||||
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
|
||||
tests["lume.clone"] = function()
|
||||
local t = {6, 7, 4, 5}
|
||||
|
Reference in New Issue
Block a user