mirror of
https://github.com/TangentFoxy/lume.git
synced 2025-07-29 11:32:20 +00:00
Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
f6174a4b68 | ||
|
d99bf9549a | ||
|
0cc52cd24e | ||
|
0a8258d608 | ||
|
95c3f8960d | ||
|
6b73aaa8ad | ||
|
1a82d308af | ||
|
17b58ec63d | ||
|
5ab25046a2 | ||
|
3f61d823ae | ||
|
0716caf6a1 | ||
|
6a160a3afe | ||
|
fff0d780bb | ||
|
283f7ee787 | ||
|
076ca7972c | ||
|
c2311f9821 | ||
|
beced85d6b | ||
|
8a2765a41b |
31
README.md
31
README.md
@@ -38,7 +38,7 @@ lume.lerp(100, 200, .5) -- Returns 150
|
|||||||
```
|
```
|
||||||
|
|
||||||
### lume.smooth(a, b, amount)
|
### 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.
|
interpolation.
|
||||||
|
|
||||||
### lume.pingpong(x)
|
### lume.pingpong(x)
|
||||||
@@ -208,6 +208,25 @@ f() -- Prints "Hello"
|
|||||||
f() -- Does nothing
|
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, ...)
|
### lume.time(fn, ...)
|
||||||
Inserts the arguments into function `fn` and calls it. Returns the time in
|
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.
|
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.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)
|
### lume.lambda(str)
|
||||||
Takes a string lambda and returns a function. `str` should be a list of
|
Takes a string lambda and returns a function. `str` should be a list of
|
||||||
comma-separated parameters, followed by `->`, followed by the expression which
|
comma-separated parameters, followed by `->`, followed by the expression which
|
||||||
|
67
lume.lua
67
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.3.0" }
|
local lume = { _version = "1.4.1" }
|
||||||
|
|
||||||
local pairs, ipairs = pairs, ipairs
|
local pairs, ipairs = pairs, ipairs
|
||||||
local type, assert, unpack = type, assert, unpack or table.unpack
|
local type, assert, unpack = type, assert, unpack or table.unpack
|
||||||
@@ -30,10 +30,13 @@ local absindex = function(len, i)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local iscallable = function(x)
|
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
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function lume.clamp(x, min, max)
|
function lume.clamp(x, min, max)
|
||||||
return x < min and min or (x > max and max or x)
|
return x < min and min or (x > max and max or x)
|
||||||
end
|
end
|
||||||
@@ -56,7 +59,8 @@ end
|
|||||||
|
|
||||||
|
|
||||||
function lume.smooth(a, b, amount)
|
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
|
return a + (b - a) * m
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -117,7 +121,7 @@ end
|
|||||||
|
|
||||||
function lume.array(...)
|
function lume.array(...)
|
||||||
local t = {}
|
local t = {}
|
||||||
for x in unpack({...}) do t[#t + 1] = x end
|
for x in ... do t[#t + 1] = x end
|
||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -266,22 +270,46 @@ function lume.once(fn, ...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function lume.time(fn, ...)
|
local memoize_fnkey = {}
|
||||||
local start = os.clock()
|
local memoize_nil = {}
|
||||||
local rtn = {fn(...)}
|
|
||||||
return (os.clock() - start), unpack(rtn)
|
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
|
end
|
||||||
|
|
||||||
|
|
||||||
function lume.combine(...)
|
function lume.combine(...)
|
||||||
local funcs = {...}
|
local funcs = {}
|
||||||
assert(lume.all(funcs, iscallable), "expected all arguments to be functions")
|
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(...)
|
return function(...)
|
||||||
for _, f in ipairs(funcs) do f(...) end
|
for _, f in ipairs(funcs) do f(...) end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.time(fn, ...)
|
||||||
|
local start = os.clock()
|
||||||
|
local rtn = {fn(...)}
|
||||||
|
return (os.clock() - start), unpack(rtn)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
local lambda_cache = {}
|
local lambda_cache = {}
|
||||||
|
|
||||||
function lume.lambda(str)
|
function lume.lambda(str)
|
||||||
@@ -344,13 +372,14 @@ end
|
|||||||
|
|
||||||
|
|
||||||
function lume.trace(...)
|
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 info = debug.getinfo(2, "Sl")
|
||||||
local head = "[" .. info.short_src .. ":" .. info.currentline .. "] "
|
local t = { "[" .. info.short_src .. ":" .. info.currentline .. "]" }
|
||||||
print(head .. table.concat(lume.map({...}, stringify), " "))
|
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
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -406,9 +435,9 @@ end
|
|||||||
|
|
||||||
|
|
||||||
function lume.rgba(color)
|
function lume.rgba(color)
|
||||||
local a = math_floor((color / 2 ^ 24) % 256)
|
local a = math_floor((color / 16777216) % 256)
|
||||||
local r = math_floor((color / 2 ^ 16) % 256)
|
local r = math_floor((color / 65536) % 256)
|
||||||
local g = math_floor((color / 2 ^ 08) % 256)
|
local g = math_floor((color / 256) % 256)
|
||||||
local b = math_floor((color) % 256)
|
local b = math_floor((color) % 256)
|
||||||
return r, g, b, a
|
return r, g, b, a
|
||||||
end
|
end
|
||||||
|
@@ -279,11 +279,18 @@ tests["lume.once"] = function()
|
|||||||
tester.test.error( lume.once, 123 )
|
tester.test.error( lume.once, 123 )
|
||||||
end
|
end
|
||||||
|
|
||||||
-- lume.time
|
-- lume.memoize
|
||||||
tests["lume.time"] = function()
|
tests["lume.memoize"] = function()
|
||||||
local t, a, b, c = lume.time(function(x) return 50, 60, x end, 70)
|
local f = lume.memoize(
|
||||||
testeq( type(t), "number" )
|
function(a, b, c)
|
||||||
testeq( {a, b, c}, {50, 60, 70} )
|
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
|
end
|
||||||
|
|
||||||
-- lume.combine
|
-- lume.combine
|
||||||
@@ -294,6 +301,17 @@ tests["lume.combine"] = function()
|
|||||||
local fn = lume.combine(a, b)
|
local fn = lume.combine(a, b)
|
||||||
fn(10, 20)
|
fn(10, 20)
|
||||||
testeq( acc, 230 )
|
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
|
end
|
||||||
|
|
||||||
-- lume.lambda
|
-- lume.lambda
|
||||||
@@ -359,11 +377,11 @@ tests["lume.trace"] = function()
|
|||||||
print = function(x)
|
print = function(x)
|
||||||
file, line, msg = x:match("%[(.-):(.-)%] (.*)")
|
file, line, msg = x:match("%[(.-):(.-)%] (.*)")
|
||||||
end
|
end
|
||||||
lume.trace("Hi world", 123.456, 1)
|
lume.trace("Hi world", 123.456, 1, nil)
|
||||||
print = oldprint
|
print = oldprint
|
||||||
testeq( file:match(".lua$"), ".lua" )
|
testeq( file:match(".lua$"), ".lua" )
|
||||||
testeq( tonumber(line) ~= nil, true )
|
testeq( tonumber(line) ~= nil, true )
|
||||||
testeq( msg, "Hi world 123.46 1" )
|
testeq( msg, "Hi world 123.46 1 nil" )
|
||||||
end
|
end
|
||||||
|
|
||||||
-- lume.dostring
|
-- lume.dostring
|
||||||
|
Reference in New Issue
Block a user