15 Commits

Author SHA1 Message Date
rxi
a4fe134985 Version 1.1.2 2014-03-08 16:10:16 +00:00
rxi
cbff46bdfb Fixed empty-table test for lume.invert() 2014-03-08 16:08:58 +00:00
rxi
5e15a57f37 Moved lume.invert()'s test func to match order in lume.lua 2014-03-08 15:47:28 +00:00
rxi
2f388bc2c7 Moved lume.invert() to be near lume.clone()
This makes the series of python-like functions (map, all, any, reduce,
set, filter) consecutive in the source and readme, which seems like a
more logical order.
2014-03-08 15:23:13 +00:00
rxi
8f4d1b158f Added empty-table test for lume.set() 2014-03-08 14:48:04 +00:00
rxi
1bbb795a89 Changed lume.set() to make use of lume.invert() 2014-03-08 14:47:27 +00:00
rxi
2c71079ef3 Added tests for lume.invert() 2014-03-08 14:45:18 +00:00
rxi
8a76fd7595 Added lume.invert() 2014-03-08 14:45:08 +00:00
rxi
b4bea5f4e0 Added new tests for lume.reduce() 2014-03-06 19:41:14 +00:00
rxi
51189d190d Made first argument in lume.reduce() optional 2014-03-06 19:33:13 +00:00
rxi
e6d47627cd Increased version to 1.1.1 2014-03-05 21:52:28 +00:00
rxi
05828bd840 Fixed module's table not updating in lume.hotswap()
Changed the returned values from pcall() in lume.hotswap() to be handled
correctly -- the second return value being the wrapped-function's first
return value. This bug would effect hotswapped modules which used the
modern approach of returning a table rather than creating globals.
2014-03-05 19:11:14 +00:00
rxi
83a051aadb Improved lume.clamp()'s performance on non-JIT
Removed use of the functions math.max() and math.min() and replaced them
with and/ors:

[5000000 calls, Lua5.2]
old func: 1.37 seconds
new func: 0.53 seconds
2014-03-05 12:44:19 +00:00
rxi
2699094218 Added better tests for lume.distance() 2014-03-05 12:22:05 +00:00
rxi
a08436445d Improved lume.distance()'s performance on non-JIT
Replaced use of the exponent operator with multiplications, yielding a
performance increase on non-JIT Lua:

[5000000 calls, Lua 5.1]
old func: 2.03 seconds
new func: 1.17 seconds

[5000000 calls, Lua 5.2]
old func: 1.60 seconds
new func: 0.89 seconds

[2000000000 calls, LuaJIT 2.0.2]
old func: 0.89 seconds
new func: 0.89 seconds
2014-03-05 12:18:49 +00:00
3 changed files with 54 additions and 19 deletions

View File

@@ -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`.

View File

@@ -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)

View File

@@ -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}