18 Commits

Author SHA1 Message Date
rxi
f6174a4b68 Version 1.4.1 2014-07-17 22:02:39 +01:00
rxi
d99bf9549a Removed unnecessary pack & unpack from lume.array() 2014-06-16 21:07:40 +01:00
rxi
0cc52cd24e Version 1.4.0 2014-05-18 08:46:28 +01:00
rxi
0a8258d608 Made some minor formatting changes 2014-05-07 08:21:36 +01:00
rxi
95c3f8960d Merge pull request #3 from icrawler/Dev2
Stripped all trailing whitespace, changed ease function, and pre-computed po2
2014-05-07 08:18:22 +01:00
Phoenix Enero
6b73aaa8ad Pre-computed powers of two in lume.rgba 2014-05-07 12:11:29 +08:00
Phoenix Enero
1a82d308af Changed ease function from cosine to cubic 2014-05-07 12:11:26 +08:00
Phoenix Enero
17b58ec63d Stripped all trailing whitespace 2014-05-07 12:11:15 +08:00
rxi
5ab25046a2 Renamed local memoize_nilkey -> memoize_nil 2014-05-01 21:27:57 +01:00
rxi
3f61d823ae Changed position of lume.time() function 2014-05-01 19:23:44 +01:00
rxi
0716caf6a1 Added lume.memoize(), updated README.md and tests 2014-05-01 18:58:29 +01:00
rxi
6a160a3afe Fixed iscallable() to handle the lack of metatable properly 2014-04-28 12:52:29 +01:00
rxi
fff0d780bb Updated README.md and tests for lume.combine() 2014-04-28 12:48:44 +01:00
rxi
283f7ee787 Changed lume.combine() to ignore nil args 2014-04-28 12:46:58 +01:00
rxi
076ca7972c Version 1.3.1 2014-04-23 20:11:00 +01:00
rxi
c2311f9821 Added missing tostring() call on args in lume.trace() 2014-04-19 09:06:33 +01:00
rxi
beced85d6b Updated test for lume.trace() 2014-04-18 19:16:33 +01:00
rxi
8a2765a41b Fixed lume.trace() to handle and print nil argument 2014-04-18 19:15:13 +01:00
3 changed files with 102 additions and 46 deletions

View File

@@ -38,7 +38,7 @@ lume.lerp(100, 200, .5) -- Returns 150
```
### lume.smooth(a, b, amount)
Similar to `lume.lerp()` but uses cosine interpolation instead of linear
Similar to `lume.lerp()` but uses cubic interpolation instead of linear
interpolation.
### lume.pingpong(x)
@@ -208,6 +208,25 @@ f() -- Prints "Hello"
f() -- Does nothing
```
### lume.memoize(fn)
Returns a wrapper function to `fn` where the results for any given set of
arguments are cached. `lume.memoize()` is useful when used on functions with
slow-running computations.
```lua
fib = lume.memoize(function(n) return n < 2 and n or fib(n-1) + fib(n-2) end)
```
### lume.combine(...)
Creates a wrapper function which calls each supplied argument in the order they
were passed to `lume.combine()`; nil arguments are ignored. The wrapper
function passes its own arguments to each of its wrapped functions when it is
called.
```lua
local f = lume.combine(function(a, b) print(a + b) end,
function(a, b) print(a * b) end)
f(3, 4) -- Prints "7" then "12" on a new line
```
### lume.time(fn, ...)
Inserts the arguments into function `fn` and calls it. Returns the time in
seconds the function `fn` took to execute followed by `fn`'s returned values.
@@ -215,16 +234,6 @@ seconds the function `fn` took to execute followed by `fn`'s returned values.
lume.time(function(x) return x end, "hello") -- Returns 0, "hello"
```
### lume.combine(...)
Creates a wrapper function which calls each supplied argument in the order they
were passed to `lume.combine`. The wrapper function passes its own arguments to
each of its wrapped functions when it is called.
```lua
local f = lume.combine(function(a, b) print(a + b) end,
function(a, b) print(a * b) end)
f(3, 4) -- Prints "7" then "12" on a new line
```
### lume.lambda(str)
Takes a string lambda and returns a function. `str` should be a list of
comma-separated parameters, followed by `->`, followed by the expression which

View File

@@ -7,7 +7,7 @@
-- under the terms of the MIT license. See LICENSE for details.
--
local lume = { _version = "1.3.0" }
local lume = { _version = "1.4.1" }
local pairs, ipairs = pairs, ipairs
local type, assert, unpack = type, assert, unpack or table.unpack
@@ -30,10 +30,13 @@ local absindex = function(len, i)
end
local iscallable = function(x)
return type(x) == "function" or getmetatable(x).__call ~= nil
if type(x) == "function" then return true end
local mt = getmetatable(x)
return mt and mt.__call ~= nil
end
function lume.clamp(x, min, max)
return x < min and min or (x > max and max or x)
end
@@ -56,7 +59,8 @@ end
function lume.smooth(a, b, amount)
local m = (1 - math_cos(lume.clamp(amount, 0, 1) * math_pi)) / 2
local t = lume.clamp(amount, 0, 1)
local m = t * t * (3 - 2 * t)
return a + (b - a) * m
end
@@ -117,7 +121,7 @@ end
function lume.array(...)
local t = {}
for x in unpack({...}) do t[#t + 1] = x end
for x in ... do t[#t + 1] = x end
return t
end
@@ -133,7 +137,7 @@ end
function lume.map(t, fn)
local rtn = {}
local rtn = {}
for k, v in pairs(t) do rtn[k] = fn(v) end
return rtn
end
@@ -167,7 +171,7 @@ end
function lume.set(t, retainkeys)
local rtn = {}
for k, v in pairs(lume.invert(t)) do
for k, v in pairs(lume.invert(t)) do
rtn[retainkeys and v or (#rtn + 1)] = k
end
return rtn
@@ -250,7 +254,7 @@ function lume.fn(fn, ...)
local args = {...}
return function(...)
local a = lume.merge(lume.clone(args), {...})
return fn(unpack(a))
return fn(unpack(a))
end
end
@@ -266,22 +270,46 @@ function lume.once(fn, ...)
end
function lume.time(fn, ...)
local start = os.clock()
local rtn = {fn(...)}
return (os.clock() - start), unpack(rtn)
local memoize_fnkey = {}
local memoize_nil = {}
function lume.memoize(fn)
local cache = {}
return function(...)
local c = cache
for i = 1, select("#", ...) do
local a = select(i, ...) or memoize_nil
c[a] = c[a] or {}
c = c[a]
end
c[memoize_fnkey] = c[memoize_fnkey] or {fn(...)}
return unpack(c[memoize_fnkey])
end
end
function lume.combine(...)
local funcs = {...}
assert(lume.all(funcs, iscallable), "expected all arguments to be functions")
local funcs = {}
for i = 1, select("#", ...) do
local fn = select(i, ...)
if fn ~= nil then
assert(iscallable(fn), "expected a function or nil")
funcs[#funcs + 1] = fn
end
end
return function(...)
for _, f in ipairs(funcs) do f(...) end
end
end
function lume.time(fn, ...)
local start = os.clock()
local rtn = {fn(...)}
return (os.clock() - start), unpack(rtn)
end
local lambda_cache = {}
function lume.lambda(str)
@@ -336,21 +364,22 @@ end
function lume.format(str, vars)
if not vars then return str end
local f = function(x)
return tostring(vars[x] or vars[tonumber(x)] or "{" .. x .. "}")
local f = function(x)
return tostring(vars[x] or vars[tonumber(x)] or "{" .. x .. "}")
end
return (str:gsub("{(.-)}", f))
end
function lume.trace(...)
local function stringify(x)
x = (type(x) == "number") and lume.round(x, .01) or x
return tostring(x)
end
local info = debug.getinfo(2, "Sl")
local head = "[" .. info.short_src .. ":" .. info.currentline .. "] "
print(head .. table.concat(lume.map({...}, stringify), " "))
local t = { "[" .. info.short_src .. ":" .. info.currentline .. "]" }
for i = 1, select("#", ...) do
local x = select(i, ...)
x = (type(x) == "number") and lume.round(x, .01) or (x or "nil")
t[#t + 1] = tostring(x)
end
print(table.concat(t, " "))
end
@@ -373,7 +402,7 @@ function lume.hotswap(modname)
local oldglobal = lume.clone(_G)
local updated = {}
local function update(old, new)
if updated[old] then return end
if updated[old] then return end
updated[old] = true
local oldmt, newmt = getmetatable(old), getmetatable(new)
if oldmt and newmt then update(oldmt, newmt) end
@@ -393,7 +422,7 @@ function lume.hotswap(modname)
local newmod = require(modname)
if type(oldmod) == "table" then update(oldmod, newmod) end
for k, v in pairs(oldglobal) do
if v ~= _G[k] and type(v) == "table" then
if v ~= _G[k] and type(v) == "table" then
update(v, _G[k])
_G[k] = v
end
@@ -406,9 +435,9 @@ end
function lume.rgba(color)
local a = math_floor((color / 2 ^ 24) % 256)
local r = math_floor((color / 2 ^ 16) % 256)
local g = math_floor((color / 2 ^ 08) % 256)
local a = math_floor((color / 16777216) % 256)
local r = math_floor((color / 65536) % 256)
local g = math_floor((color / 256) % 256)
local b = math_floor((color) % 256)
return r, g, b, a
end

View File

@@ -279,11 +279,18 @@ tests["lume.once"] = function()
tester.test.error( lume.once, 123 )
end
-- lume.time
tests["lume.time"] = function()
local t, a, b, c = lume.time(function(x) return 50, 60, x end, 70)
testeq( type(t), "number" )
testeq( {a, b, c}, {50, 60, 70} )
-- lume.memoize
tests["lume.memoize"] = function()
local f = lume.memoize(
function(a, b, c)
return tostring(a) .. tostring(b) .. tostring(c)
end)
testeq( f("hello", nil, 15), "hellonil15" )
testeq( f("hello", nil, 15), "hellonil15" )
testeq( f(), "nilnilnil" )
testeq( f(), "nilnilnil" )
local f2 = lume.memoize(function() end)
testeq( f2(), nil )
end
-- lume.combine
@@ -294,6 +301,17 @@ tests["lume.combine"] = function()
local fn = lume.combine(a, b)
fn(10, 20)
testeq( acc, 230 )
acc = 0
fn = lume.combine(nil, a, nil, b, nil)
fn(10, 20)
testeq( acc, 230 )
end
-- lume.time
tests["lume.time"] = function()
local t, a, b, c = lume.time(function(x) return 50, 60, x end, 70)
testeq( type(t), "number" )
testeq( {a, b, c}, {50, 60, 70} )
end
-- lume.lambda
@@ -359,11 +377,11 @@ tests["lume.trace"] = function()
print = function(x)
file, line, msg = x:match("%[(.-):(.-)%] (.*)")
end
lume.trace("Hi world", 123.456, 1)
lume.trace("Hi world", 123.456, 1, nil)
print = oldprint
testeq( file:match(".lua$"), ".lua" )
testeq( tonumber(line) ~= nil, true )
testeq( msg, "Hi world 123.46 1" )
testeq( file:match(".lua$"), ".lua" )
testeq( tonumber(line) ~= nil, true )
testeq( msg, "Hi world 123.46 1 nil" )
end
-- lume.dostring