mirror of
https://github.com/TangentFoxy/lume.git
synced 2025-07-28 11:02:20 +00:00
Compare commits
23 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
0e4256ef0d | ||
|
bcad07fc5f | ||
|
8f1267f967 | ||
|
de37bd6d65 | ||
|
ac920c8f5e | ||
|
5d258d4fd0 | ||
|
df26e7939d | ||
|
16e370cdf0 | ||
|
0b991d7ea5 | ||
|
f6174a4b68 | ||
|
d99bf9549a | ||
|
0cc52cd24e | ||
|
0a8258d608 | ||
|
95c3f8960d | ||
|
6b73aaa8ad | ||
|
1a82d308af | ||
|
17b58ec63d | ||
|
5ab25046a2 | ||
|
3f61d823ae | ||
|
0716caf6a1 | ||
|
6a160a3afe | ||
|
fff0d780bb | ||
|
283f7ee787 |
53
README.md
53
README.md
@@ -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)
|
||||
@@ -178,6 +178,20 @@ 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.first(t [, n])
|
||||
Returns the first element of an array or nil if the array is empty. If `n` is
|
||||
specificed an array of the first `n` elements is returned.
|
||||
```lua
|
||||
lume.first({"a", "b", "c"}) -- Returns "a"
|
||||
```
|
||||
|
||||
### lume.last(t [, n])
|
||||
Returns the last element of an array or nil if the array is empty. If `n` is
|
||||
specificed an array of the last `n` elements is returned.
|
||||
```lua
|
||||
lume.last({"a", "b", "c"}) -- Returns "c"
|
||||
```
|
||||
|
||||
### lume.invert(t)
|
||||
Returns a copy of the table where the keys have become the values and the
|
||||
values the keys.
|
||||
@@ -185,6 +199,9 @@ values the keys.
|
||||
lume.invert({a = "x", b = "y"}) -- returns {x = "a", y = "b"}
|
||||
```
|
||||
|
||||
### lume.keys(t)
|
||||
Returns an array containing each key of the table.
|
||||
|
||||
### lume.clone(t)
|
||||
Returns a shallow copy of the table `t`.
|
||||
|
||||
@@ -208,6 +225,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 +251,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
|
||||
@@ -324,6 +350,11 @@ lume.chain({1, 2, 3, 4})
|
||||
:map(function(x) return -x end)
|
||||
:result() -- Returns { -2, -4 }
|
||||
```
|
||||
The table returned by the `lume` module, when called, acts in the same manner
|
||||
as calling `lume.chain()`.
|
||||
```lua
|
||||
lume({1, 2, 3}):each(print) -- Prints 1, 2 then 3 on separate lines
|
||||
```
|
||||
|
||||
|
||||
## License
|
||||
|
118
lume.lua
118
lume.lua
@@ -7,7 +7,7 @@
|
||||
-- under the terms of the MIT license. See LICENSE for details.
|
||||
--
|
||||
|
||||
local lume = { _version = "1.3.1" }
|
||||
local lume = { _version = "1.5.0" }
|
||||
|
||||
local pairs, ipairs = pairs, ipairs
|
||||
local type, assert, unpack = type, assert, unpack or table.unpack
|
||||
@@ -30,7 +30,27 @@ 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
|
||||
|
||||
local identity = function(x)
|
||||
return x
|
||||
end
|
||||
|
||||
local iteratee = function(x)
|
||||
if x == nil then return identity end
|
||||
if iscallable(x) then return x end
|
||||
if type(x) == "table" then
|
||||
return function(z)
|
||||
for k, v in pairs(x) do
|
||||
if z[k] ~= v then return false end
|
||||
end
|
||||
return true
|
||||
end
|
||||
end
|
||||
return function(z) return z[x] end
|
||||
end
|
||||
|
||||
|
||||
@@ -57,7 +77,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
|
||||
|
||||
@@ -118,7 +139,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
|
||||
|
||||
@@ -134,14 +155,15 @@ end
|
||||
|
||||
|
||||
function lume.map(t, fn)
|
||||
local rtn = {}
|
||||
fn = iteratee(fn)
|
||||
local rtn = {}
|
||||
for k, v in pairs(t) do rtn[k] = fn(v) end
|
||||
return rtn
|
||||
end
|
||||
|
||||
|
||||
function lume.all(t, fn)
|
||||
fn = fn or function(x) return x end
|
||||
fn = iteratee(fn)
|
||||
for k, v in pairs(t) do
|
||||
if not fn(v) then return false end
|
||||
end
|
||||
@@ -150,7 +172,7 @@ end
|
||||
|
||||
|
||||
function lume.any(t, fn)
|
||||
fn = fn or function(x) return x end
|
||||
fn = iteratee(fn)
|
||||
for k, v in pairs(t) do
|
||||
if fn(v) then return true end
|
||||
end
|
||||
@@ -168,7 +190,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
|
||||
@@ -176,6 +198,7 @@ end
|
||||
|
||||
|
||||
function lume.filter(t, fn, retainkeys)
|
||||
fn = iteratee(fn)
|
||||
local rtn = {}
|
||||
for k, v in pairs(t) do
|
||||
if fn(v) then rtn[retainkeys and k or (#rtn + 1)] = v end
|
||||
@@ -201,6 +224,7 @@ end
|
||||
|
||||
|
||||
function lume.match(t, fn)
|
||||
fn = iteratee(fn)
|
||||
for k, v in pairs(t) do
|
||||
if fn(v) then return v, k end
|
||||
end
|
||||
@@ -211,6 +235,7 @@ end
|
||||
function lume.count(t, fn)
|
||||
local count = 0
|
||||
if fn then
|
||||
fn = iteratee(fn)
|
||||
for k, v in pairs(t) do
|
||||
if fn(v) then count = count + 1 end
|
||||
end
|
||||
@@ -232,6 +257,18 @@ function lume.slice(t, i, j)
|
||||
end
|
||||
|
||||
|
||||
function lume.first(t, n)
|
||||
if not n then return t[1] end
|
||||
return lume.slice(t, 1, n)
|
||||
end
|
||||
|
||||
|
||||
function lume.last(t, n)
|
||||
if not n then return t[#t] end
|
||||
return lume.slice(t, -n, -1)
|
||||
end
|
||||
|
||||
|
||||
function lume.invert(t)
|
||||
local rtn = {}
|
||||
for k, v in pairs(t) do rtn[v] = k end
|
||||
@@ -239,6 +276,13 @@ function lume.invert(t)
|
||||
end
|
||||
|
||||
|
||||
function lume.keys(t)
|
||||
local rtn = {}
|
||||
for k, v in pairs(t) do rtn[#rtn + 1] = k end
|
||||
return rtn
|
||||
end
|
||||
|
||||
|
||||
function lume.clone(t)
|
||||
local rtn = {}
|
||||
for k, v in pairs(t) do rtn[k] = v end
|
||||
@@ -251,7 +295,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
|
||||
|
||||
@@ -267,22 +311,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)
|
||||
@@ -337,8 +405,8 @@ 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
|
||||
@@ -375,7 +443,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
|
||||
@@ -395,7 +463,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
|
||||
@@ -408,9 +476,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
|
||||
@@ -430,5 +498,11 @@ function lume.chain(value)
|
||||
return setmetatable({ _value = value }, chain_mt)
|
||||
end
|
||||
|
||||
setmetatable(lume, {
|
||||
__call = function(t, ...)
|
||||
return lume.chain(...)
|
||||
end
|
||||
})
|
||||
|
||||
|
||||
return lume
|
||||
|
@@ -141,6 +141,8 @@ end
|
||||
tests["lume.map"] = function()
|
||||
testeq( lume.map({1, 2, 3}, function(x) return x * 2 end), {2, 4, 6} )
|
||||
testeq( lume.map({a=2,b=3}, function(x) return x * 2 end), {a=4,b=6} )
|
||||
local t = {{ id = 10 }, { id = 20 }, { id = 30 }}
|
||||
testeq( lume.map(t, "id"), { 10, 20, 30 })
|
||||
end
|
||||
|
||||
-- lume.all
|
||||
@@ -149,6 +151,10 @@ tests["lume.all"] = function()
|
||||
testeq( lume.all({true, true, true, true}), true )
|
||||
testeq( lume.all({2, 3, 4, 5}, function(x) return x % 2 == 0 end), false )
|
||||
testeq( lume.all({2, 4, 6, 8}, function(x) return x % 2 == 0 end), true )
|
||||
testeq( lume.all({{ x = 1 }, {}, { x = 3 }}, "x"), false )
|
||||
testeq( lume.all({{ x = 1 }, { x = 2 }, { x = 3 }}, "x"), true )
|
||||
testeq( lume.all({{ x = 1 }, { x = 2 }, { x = 3 }}, { x = 2 }), false )
|
||||
testeq( lume.all({{ x = 2 }, { x = 2 }, { x = 2 }}, { x = 2 }), true )
|
||||
end
|
||||
|
||||
-- lume.any
|
||||
@@ -157,6 +163,9 @@ tests["lume.any"] = function()
|
||||
testeq( lume.any({false, false, false}), false )
|
||||
testeq( lume.any({2, 3, 4, 5}, function(x) return x % 2 == 0 end), true )
|
||||
testeq( lume.any({1, 3, 5, 7}, function(x) return x % 2 == 0 end), false )
|
||||
local t = {{ id = 10 }, { id = 20 }, { id = 30 }}
|
||||
testeq( lume.any(t, { id = 10 }), true )
|
||||
testeq( lume.any(t, { id = 40 }), false )
|
||||
end
|
||||
|
||||
-- lume.reduce
|
||||
@@ -189,6 +198,8 @@ tests["lume.filter"] = function()
|
||||
testeq( t, {2, 4} )
|
||||
local t = lume.filter({a=1, b=2, c=3}, function(x) return x == 2 end, true)
|
||||
testeq( t, {b=2} )
|
||||
local t = lume.filter({{ x=1, y=1 }, { x=2, y=2 }, { x=1, y=3 }}, { x = 1 })
|
||||
testeq( t, {{ x=1, y=1 }, {x=1, y=3}} )
|
||||
end
|
||||
|
||||
-- lume.merge
|
||||
@@ -208,6 +219,7 @@ end
|
||||
tests["lume.match"] = function()
|
||||
local t = { "a", "b", "c", "d" }
|
||||
local t2 = { a = 1, b = 2, c = 3, d = 4 }
|
||||
local t3 = { {x=1, y=2}, {x=3, y=4}, {x=5, y=6} }
|
||||
local v, k = lume.match(t, function(x) return x > "c" end)
|
||||
testeq( v, "d" )
|
||||
testeq( k, 4 )
|
||||
@@ -220,6 +232,8 @@ tests["lume.match"] = function()
|
||||
local v, k = lume.match(t2, function(x) return x > 5 end)
|
||||
testeq( v, nil )
|
||||
testeq( k, nil )
|
||||
local v, k = lume.match(t3, { x = 3, y = 4 })
|
||||
testeq( k, 2 )
|
||||
end
|
||||
|
||||
-- lume.count
|
||||
@@ -229,6 +243,10 @@ tests["lume.count"] = function()
|
||||
testeq( lume.count(t, function(x) return x % 2 == 0 end ), 3 )
|
||||
local a = { 5, 6, 7, 8, 9 }
|
||||
testeq( lume.count(a), #a )
|
||||
local t = { { n = 20 }, { n = 30 }, { n = 40 }, { n = 20 } }
|
||||
testeq( lume.count(t, { n = 20 }), 2 )
|
||||
testeq( lume.count(t, { n = 30 }), 1 )
|
||||
testeq( lume.count(t, { n = 50 }), 0 )
|
||||
end
|
||||
|
||||
-- lume.slice
|
||||
@@ -248,14 +266,40 @@ tests["lume.slice"] = function()
|
||||
testeq( lume.slice({"a", "b", "c", "d", "e"}, -3, 900), {"c", "d", "e"} )
|
||||
end
|
||||
|
||||
-- lume.first
|
||||
tests["lume.first"] = function()
|
||||
local t = { "a", "b", "c", "d", "e" }
|
||||
testeq( lume.first(t), "a" )
|
||||
testeq( lume.first(t, 1), { "a" } )
|
||||
testeq( lume.first(t, 2), { "a", "b" } )
|
||||
end
|
||||
|
||||
-- lume.last
|
||||
tests["lume.last"] = function()
|
||||
local t = { "a", "b", "c", "d", "e" }
|
||||
testeq( lume.last(t), "e" )
|
||||
testeq( lume.last(t, 1), { "e" } )
|
||||
testeq( lume.last(t, 2), { "d", "e" } )
|
||||
end
|
||||
|
||||
-- lume.invert
|
||||
tests["lume_invert"] = function()
|
||||
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.keys
|
||||
tests["lume.keys"] = function()
|
||||
testeq( lume.keys({}), {} )
|
||||
local t = lume.keys({ aaa = 1, bbb = 2, ccc = 3 })
|
||||
table.sort(t)
|
||||
testeq( t, {"aaa", "bbb", "ccc"} )
|
||||
local t = lume.keys({ "x", "x", "x" })
|
||||
testeq( t, {1, 2, 3} )
|
||||
end
|
||||
|
||||
-- lume.clone
|
||||
tests["lume.clone"] = function()
|
||||
local t = {6, 7, 4, 5}
|
||||
@@ -279,11 +323,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 +345,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
|
||||
@@ -399,6 +461,8 @@ tests["lume.chain"] = function()
|
||||
local t = lume.chain({1, 2}):map(function(x) return x * 2 end):result()
|
||||
testeq( t, { 2, 4 } )
|
||||
testeq( lume.chain(10):result(), 10 )
|
||||
local t = lume({1, 2}):map(function(x) return x * 2 end):result()
|
||||
testeq( t, { 2, 4 } )
|
||||
end
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user