2014-02-27 20:19:10 +00:00
|
|
|
--
|
|
|
|
-- lume
|
|
|
|
--
|
2015-02-28 21:03:42 +00:00
|
|
|
-- Copyright (c) 2015 rxi
|
2014-02-27 20:19:10 +00:00
|
|
|
--
|
|
|
|
-- This library is free software; you can redistribute it and/or modify it
|
|
|
|
-- under the terms of the MIT license. See LICENSE for details.
|
|
|
|
--
|
|
|
|
|
2015-07-31 17:58:24 +00:00
|
|
|
local lume = { _version = "2.2.0" }
|
2014-02-27 20:19:10 +00:00
|
|
|
|
2014-03-18 12:07:53 +00:00
|
|
|
local pairs, ipairs = pairs, ipairs
|
2014-03-29 00:18:56 +00:00
|
|
|
local type, assert, unpack = type, assert, unpack or table.unpack
|
2014-03-18 12:07:53 +00:00
|
|
|
local tostring, tonumber = tostring, tonumber
|
|
|
|
local math_floor = math.floor
|
|
|
|
local math_ceil = math.ceil
|
|
|
|
local math_random = math.random
|
|
|
|
local math_cos = math.cos
|
2015-01-12 19:51:24 +00:00
|
|
|
local math_atan2 = math.atan2 or math.atan
|
2014-03-18 12:07:53 +00:00
|
|
|
local math_sqrt = math.sqrt
|
|
|
|
local math_abs = math.abs
|
|
|
|
local math_pi = math.pi
|
|
|
|
|
2015-01-10 14:13:07 +00:00
|
|
|
local noop = function()
|
|
|
|
end
|
|
|
|
|
|
|
|
local identity = function(x)
|
|
|
|
return x
|
|
|
|
end
|
|
|
|
|
2014-03-19 21:05:34 +00:00
|
|
|
local patternescape = function(str)
|
|
|
|
return str:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", "%%%1")
|
|
|
|
end
|
|
|
|
|
2014-04-01 11:17:55 +00:00
|
|
|
local absindex = function(len, i)
|
|
|
|
return i < 0 and (len + i + 1) or i
|
|
|
|
end
|
|
|
|
|
2014-04-17 11:13:50 +00:00
|
|
|
local iscallable = function(x)
|
2014-04-28 11:52:29 +00:00
|
|
|
if type(x) == "function" then return true end
|
|
|
|
local mt = getmetatable(x)
|
|
|
|
return mt and mt.__call ~= nil
|
2014-04-03 19:09:40 +00:00
|
|
|
end
|
|
|
|
|
2015-01-10 14:13:07 +00:00
|
|
|
local isarray = function(x)
|
2015-02-08 15:01:17 +00:00
|
|
|
return (type(x) == "table" and x[1] ~= nil) and true or false
|
2015-01-10 14:13:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local getiter = function(x)
|
2015-02-24 20:42:44 +00:00
|
|
|
if isarray(x) then
|
2015-01-10 14:13:07 +00:00
|
|
|
return ipairs
|
2015-02-24 20:42:44 +00:00
|
|
|
elseif type(x) == "table" then
|
2015-01-10 14:13:07 +00:00
|
|
|
return pairs
|
|
|
|
end
|
2015-02-24 20:42:44 +00:00
|
|
|
error("expected table", 3)
|
2014-12-12 18:56:13 +00:00
|
|
|
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
|
|
|
|
|
2014-02-27 20:19:10 +00:00
|
|
|
|
2014-04-18 18:09:59 +00:00
|
|
|
|
2014-02-27 20:19:10 +00:00
|
|
|
function lume.clamp(x, min, max)
|
2014-03-05 12:41:40 +00:00
|
|
|
return x < min and min or (x > max and max or x)
|
2014-02-27 20:19:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-03-02 21:29:23 +00:00
|
|
|
function lume.round(x, increment)
|
|
|
|
if increment then return lume.round(x / increment) * increment end
|
2015-05-12 19:30:05 +00:00
|
|
|
return x >= 0 and math_floor(x + .5) or math_ceil(x - .5)
|
2014-02-27 20:19:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function lume.sign(x)
|
|
|
|
return x < 0 and -1 or 1
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function lume.lerp(a, b, amount)
|
|
|
|
return a + (b - a) * lume.clamp(amount, 0, 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function lume.smooth(a, b, amount)
|
2014-05-06 01:39:53 +00:00
|
|
|
local t = lume.clamp(amount, 0, 1)
|
2014-05-07 07:21:36 +00:00
|
|
|
local m = t * t * (3 - 2 * t)
|
2014-02-27 20:19:10 +00:00
|
|
|
return a + (b - a) * m
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-02-27 23:35:07 +00:00
|
|
|
function lume.pingpong(x)
|
2014-03-18 12:07:53 +00:00
|
|
|
return 1 - math_abs(1 - x % 2)
|
2014-02-27 20:19:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-03-04 22:53:31 +00:00
|
|
|
function lume.distance(x1, y1, x2, y2, squared)
|
2014-03-05 12:18:49 +00:00
|
|
|
local dx = x1 - x2
|
|
|
|
local dy = y1 - y2
|
|
|
|
local s = dx * dx + dy * dy
|
2014-03-18 12:07:53 +00:00
|
|
|
return squared and s or math_sqrt(s)
|
2014-02-27 20:19:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function lume.angle(x1, y1, x2, y2)
|
2014-03-18 12:07:53 +00:00
|
|
|
return math_atan2(y2 - y1, x2 - x1)
|
2014-02-27 20:19:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function lume.random(a, b)
|
|
|
|
if not a then a, b = 0, 1 end
|
|
|
|
if not b then b = 0 end
|
2014-03-18 12:07:53 +00:00
|
|
|
return a + math_random() * (b - a)
|
2014-02-27 20:19:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function lume.randomchoice(t)
|
2014-03-18 12:07:53 +00:00
|
|
|
return t[math_random(#t)]
|
2014-02-27 20:19:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-03-12 13:12:45 +00:00
|
|
|
function lume.weightedchoice(t)
|
|
|
|
local sum = 0
|
|
|
|
for k, v in pairs(t) do
|
|
|
|
assert(v >= 0, "weight value less than zero")
|
|
|
|
sum = sum + v
|
|
|
|
end
|
|
|
|
assert(sum ~= 0, "all weights are zero")
|
|
|
|
local rnd = lume.random(sum)
|
|
|
|
for k, v in pairs(t) do
|
|
|
|
if rnd < v then return k end
|
|
|
|
rnd = rnd - v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-02-07 22:38:08 +00:00
|
|
|
function lume.push(t, ...)
|
|
|
|
local n = select("#", ...)
|
|
|
|
for i = 1, n do
|
|
|
|
t[#t + 1] = select(i, ...)
|
|
|
|
end
|
|
|
|
return ...
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-02-07 22:48:58 +00:00
|
|
|
function lume.remove(t, x)
|
|
|
|
local iter = getiter(t)
|
|
|
|
for i, v in iter(t) do
|
|
|
|
if v == x then
|
|
|
|
if isarray(t) then
|
|
|
|
table.remove(t, i)
|
|
|
|
break
|
|
|
|
else
|
|
|
|
t[i] = nil
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return x
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-02-07 22:51:28 +00:00
|
|
|
function lume.clear(t)
|
|
|
|
local iter = getiter(t)
|
|
|
|
for k, v in iter(t) do
|
|
|
|
t[k] = nil
|
|
|
|
end
|
2015-02-08 15:12:57 +00:00
|
|
|
return t
|
2015-02-07 22:51:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-05-05 18:21:56 +00:00
|
|
|
function lume.extend(t, ...)
|
|
|
|
for i = 1, select("#", ...) do
|
|
|
|
local x = select(i, ...)
|
|
|
|
if x then
|
|
|
|
for k, v in pairs(x) do
|
|
|
|
t[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-02-27 20:19:10 +00:00
|
|
|
function lume.shuffle(t)
|
2015-01-10 16:21:57 +00:00
|
|
|
local rtn = {}
|
2014-02-27 20:19:10 +00:00
|
|
|
for i = 1, #t do
|
2015-01-10 16:21:57 +00:00
|
|
|
local r = math_random(i)
|
|
|
|
if r ~= i then
|
|
|
|
rtn[i] = rtn[r]
|
|
|
|
end
|
|
|
|
rtn[r] = t[i]
|
2014-02-27 20:19:10 +00:00
|
|
|
end
|
2015-01-10 16:21:57 +00:00
|
|
|
return rtn
|
2014-02-27 20:19:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-01-10 16:54:23 +00:00
|
|
|
function lume.sort(t, comp)
|
|
|
|
local rtn = lume.clone(t)
|
|
|
|
if comp then
|
|
|
|
if type(comp) == "string" then
|
|
|
|
table.sort(rtn, function(a, b) return a[comp] < b[comp] end)
|
|
|
|
else
|
|
|
|
table.sort(rtn, comp)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
table.sort(rtn)
|
|
|
|
end
|
|
|
|
return rtn
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-02-27 20:19:10 +00:00
|
|
|
function lume.array(...)
|
|
|
|
local t = {}
|
2014-06-16 20:07:40 +00:00
|
|
|
for x in ... do t[#t + 1] = x end
|
2014-02-27 20:19:10 +00:00
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-03-03 12:08:11 +00:00
|
|
|
function lume.each(t, fn, ...)
|
2015-01-10 14:13:07 +00:00
|
|
|
local iter = getiter(t)
|
2014-03-03 12:08:11 +00:00
|
|
|
if type(fn) == "string" then
|
2015-01-10 14:13:07 +00:00
|
|
|
for _, v in iter(t) do v[fn](v, ...) end
|
2014-03-03 12:08:11 +00:00
|
|
|
else
|
2015-01-10 14:13:07 +00:00
|
|
|
for _, v in iter(t) do fn(v, ...) end
|
2014-03-03 12:08:11 +00:00
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-02-27 20:19:10 +00:00
|
|
|
function lume.map(t, fn)
|
2014-12-12 19:45:22 +00:00
|
|
|
fn = iteratee(fn)
|
2015-01-10 14:19:29 +00:00
|
|
|
local iter = getiter(t)
|
2014-05-06 01:36:51 +00:00
|
|
|
local rtn = {}
|
2015-01-10 14:19:29 +00:00
|
|
|
for k, v in iter(t) do rtn[k] = fn(v) end
|
2014-02-27 20:19:10 +00:00
|
|
|
return rtn
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function lume.all(t, fn)
|
2014-12-12 19:45:22 +00:00
|
|
|
fn = iteratee(fn)
|
2015-01-10 14:19:29 +00:00
|
|
|
local iter = getiter(t)
|
|
|
|
for k, v in iter(t) do
|
2014-02-27 20:19:10 +00:00
|
|
|
if not fn(v) then return false end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function lume.any(t, fn)
|
2014-12-12 19:45:22 +00:00
|
|
|
fn = iteratee(fn)
|
2015-01-10 14:19:29 +00:00
|
|
|
local iter = getiter(t)
|
|
|
|
for k, v in iter(t) do
|
2014-02-27 20:19:10 +00:00
|
|
|
if fn(v) then return true end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function lume.reduce(t, fn, first)
|
2015-02-24 21:01:03 +00:00
|
|
|
local acc = first
|
|
|
|
local started = first and true or false
|
|
|
|
local iter = getiter(t)
|
|
|
|
for k, v in iter(t) do
|
|
|
|
if started then
|
|
|
|
acc = fn(acc, v)
|
|
|
|
else
|
|
|
|
acc = v
|
|
|
|
started = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert(started, "reduce of an empty table with no first value")
|
2014-03-06 19:33:13 +00:00
|
|
|
return acc
|
2014-02-27 20:19:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-01-11 14:33:56 +00:00
|
|
|
function lume.set(t)
|
2014-02-27 22:17:36 +00:00
|
|
|
local rtn = {}
|
2014-05-06 01:36:51 +00:00
|
|
|
for k, v in pairs(lume.invert(t)) do
|
2015-01-11 14:33:56 +00:00
|
|
|
rtn[#rtn + 1] = k
|
2014-03-08 14:47:27 +00:00
|
|
|
end
|
2014-02-27 22:17:36 +00:00
|
|
|
return rtn
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-02-27 22:39:09 +00:00
|
|
|
function lume.filter(t, fn, retainkeys)
|
2014-12-12 19:45:22 +00:00
|
|
|
fn = iteratee(fn)
|
2015-01-10 14:19:29 +00:00
|
|
|
local iter = getiter(t)
|
2014-02-27 20:19:10 +00:00
|
|
|
local rtn = {}
|
2015-02-20 19:24:48 +00:00
|
|
|
if retainkeys then
|
|
|
|
for k, v in iter(t) do
|
|
|
|
if fn(v) then rtn[k] = v end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
for k, v in iter(t) do
|
|
|
|
if fn(v) then rtn[#rtn + 1] = v end
|
|
|
|
end
|
2014-02-27 20:19:10 +00:00
|
|
|
end
|
|
|
|
return rtn
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-02-20 19:22:42 +00:00
|
|
|
function lume.reject(t, fn, retainkeys)
|
|
|
|
fn = iteratee(fn)
|
|
|
|
local iter = getiter(t)
|
|
|
|
local rtn = {}
|
|
|
|
if retainkeys then
|
|
|
|
for k, v in iter(t) do
|
|
|
|
if not fn(v) then rtn[k] = v end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
for k, v in iter(t) do
|
|
|
|
if not fn(v) then rtn[#rtn + 1] = v end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return rtn
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-01-10 17:29:12 +00:00
|
|
|
function lume.merge(...)
|
|
|
|
local rtn = {}
|
|
|
|
for i = 1, select("#", ...) do
|
|
|
|
local t = select(i, ...)
|
|
|
|
local iter = getiter(t)
|
|
|
|
for k, v in iter(t) do
|
|
|
|
rtn[k] = v
|
|
|
|
end
|
2014-02-27 20:19:10 +00:00
|
|
|
end
|
2015-01-10 17:29:12 +00:00
|
|
|
return rtn
|
2014-02-27 20:19:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-01-10 17:13:15 +00:00
|
|
|
function lume.concat(...)
|
|
|
|
local rtn = {}
|
|
|
|
for i = 1, select("#", ...) do
|
|
|
|
local t = select(i, ...)
|
2015-02-24 20:42:44 +00:00
|
|
|
if t ~= nil then
|
|
|
|
local iter = getiter(t)
|
|
|
|
for k, v in iter(t) do
|
|
|
|
rtn[#rtn + 1] = v
|
|
|
|
end
|
2015-01-10 17:13:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return rtn
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-02-27 20:19:10 +00:00
|
|
|
function lume.find(t, value)
|
2015-01-10 14:19:29 +00:00
|
|
|
local iter = getiter(t)
|
|
|
|
for k, v in iter(t) do
|
2014-02-27 20:19:10 +00:00
|
|
|
if v == value then return k end
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-04-04 17:08:39 +00:00
|
|
|
function lume.match(t, fn)
|
2014-12-12 19:45:22 +00:00
|
|
|
fn = iteratee(fn)
|
2015-01-10 14:19:29 +00:00
|
|
|
local iter = getiter(t)
|
|
|
|
for k, v in iter(t) do
|
2014-04-04 17:08:39 +00:00
|
|
|
if fn(v) then return v, k end
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-04-05 11:11:13 +00:00
|
|
|
function lume.count(t, fn)
|
|
|
|
local count = 0
|
2015-01-10 14:19:29 +00:00
|
|
|
local iter = getiter(t)
|
2014-04-05 11:11:13 +00:00
|
|
|
if fn then
|
2014-12-12 19:45:22 +00:00
|
|
|
fn = iteratee(fn)
|
2015-01-10 14:19:29 +00:00
|
|
|
for k, v in iter(t) do
|
2014-04-05 11:11:13 +00:00
|
|
|
if fn(v) then count = count + 1 end
|
|
|
|
end
|
|
|
|
else
|
2015-01-10 14:31:55 +00:00
|
|
|
if isarray(t) then
|
|
|
|
return #t
|
|
|
|
end
|
2015-01-10 14:19:29 +00:00
|
|
|
for k in iter(t) do count = count + 1 end
|
2014-04-05 11:11:13 +00:00
|
|
|
end
|
|
|
|
return count
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-02-27 20:19:10 +00:00
|
|
|
function lume.slice(t, i, j)
|
2014-04-01 11:17:55 +00:00
|
|
|
i = i and absindex(#t, i) or 1
|
|
|
|
j = j and absindex(#t, j) or #t
|
2014-02-27 20:19:10 +00:00
|
|
|
local rtn = {}
|
2014-04-17 11:07:59 +00:00
|
|
|
for x = i < 1 and 1 or i, j > #t and #t or j do
|
|
|
|
rtn[#rtn + 1] = t[x]
|
2014-02-27 20:19:10 +00:00
|
|
|
end
|
|
|
|
return rtn
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-12-12 21:33:41 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2014-03-08 15:18:46 +00:00
|
|
|
function lume.invert(t)
|
|
|
|
local rtn = {}
|
|
|
|
for k, v in pairs(t) do rtn[v] = k end
|
|
|
|
return rtn
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-05-09 14:39:12 +00:00
|
|
|
function lume.pick(t, ...)
|
|
|
|
local rtn = {}
|
|
|
|
for i = 1, select("#", ...) do
|
|
|
|
local k = select(i, ...)
|
|
|
|
rtn[k] = t[k]
|
|
|
|
end
|
|
|
|
return rtn
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-12-12 20:37:07 +00:00
|
|
|
function lume.keys(t)
|
|
|
|
local rtn = {}
|
2015-01-10 14:19:29 +00:00
|
|
|
local iter = getiter(t)
|
|
|
|
for k, v in iter(t) do rtn[#rtn + 1] = k end
|
2014-12-12 20:37:07 +00:00
|
|
|
return rtn
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-02-27 20:19:10 +00:00
|
|
|
function lume.clone(t)
|
2014-02-27 21:31:26 +00:00
|
|
|
local rtn = {}
|
2014-02-27 20:19:10 +00:00
|
|
|
for k, v in pairs(t) do rtn[k] = v end
|
|
|
|
return rtn
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function lume.fn(fn, ...)
|
2014-04-17 11:13:50 +00:00
|
|
|
assert(iscallable(fn), "expected a function as the first argument")
|
2015-01-10 17:29:12 +00:00
|
|
|
local args = { ... }
|
2014-03-03 13:20:51 +00:00
|
|
|
return function(...)
|
2015-01-10 17:29:12 +00:00
|
|
|
local a = lume.concat(args, { ... })
|
2014-05-06 01:36:51 +00:00
|
|
|
return fn(unpack(a))
|
2014-03-03 13:20:51 +00:00
|
|
|
end
|
2014-03-02 00:09:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function lume.once(fn, ...)
|
2014-03-03 13:25:53 +00:00
|
|
|
local fn = lume.fn(fn, ...)
|
|
|
|
local done = false
|
|
|
|
return function(...)
|
|
|
|
if done then return end
|
|
|
|
done = true
|
|
|
|
return fn(...)
|
2014-03-02 00:09:24 +00:00
|
|
|
end
|
2014-02-27 20:19:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-05-01 17:58:29 +00:00
|
|
|
local memoize_fnkey = {}
|
2014-05-01 20:27:57 +00:00
|
|
|
local memoize_nil = {}
|
2014-05-01 17:58:29 +00:00
|
|
|
|
|
|
|
function lume.memoize(fn)
|
|
|
|
local cache = {}
|
|
|
|
return function(...)
|
|
|
|
local c = cache
|
|
|
|
for i = 1, select("#", ...) do
|
2014-05-01 20:27:57 +00:00
|
|
|
local a = select(i, ...) or memoize_nil
|
2014-05-01 17:58:29 +00:00
|
|
|
c[a] = c[a] or {}
|
|
|
|
c = c[a]
|
|
|
|
end
|
|
|
|
c[memoize_fnkey] = c[memoize_fnkey] or {fn(...)}
|
|
|
|
return unpack(c[memoize_fnkey])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-04-03 19:09:40 +00:00
|
|
|
function lume.combine(...)
|
2015-02-05 21:29:38 +00:00
|
|
|
local n = select('#', ...)
|
|
|
|
if n == 0 then return noop end
|
|
|
|
if n == 1 then
|
|
|
|
local fn = select(1, ...)
|
|
|
|
if not fn then return noop end
|
|
|
|
assert(iscallable(fn), "expected a function or nil")
|
|
|
|
return fn
|
|
|
|
end
|
2014-04-28 11:46:58 +00:00
|
|
|
local funcs = {}
|
2015-02-05 21:29:38 +00:00
|
|
|
for i = 1, n do
|
2014-04-28 11:46:58 +00:00
|
|
|
local fn = select(i, ...)
|
|
|
|
if fn ~= nil then
|
|
|
|
assert(iscallable(fn), "expected a function or nil")
|
|
|
|
funcs[#funcs + 1] = fn
|
|
|
|
end
|
|
|
|
end
|
2014-04-03 19:09:40 +00:00
|
|
|
return function(...)
|
|
|
|
for _, f in ipairs(funcs) do f(...) end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-01-10 16:29:13 +00:00
|
|
|
function lume.call(fn, ...)
|
|
|
|
if fn then
|
|
|
|
return fn(...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-05-01 18:23:44 +00:00
|
|
|
function lume.time(fn, ...)
|
|
|
|
local start = os.clock()
|
|
|
|
local rtn = {fn(...)}
|
|
|
|
return (os.clock() - start), unpack(rtn)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-03-15 21:20:53 +00:00
|
|
|
local lambda_cache = {}
|
|
|
|
|
2014-03-08 20:21:44 +00:00
|
|
|
function lume.lambda(str)
|
2014-03-15 21:20:53 +00:00
|
|
|
if not lambda_cache[str] then
|
|
|
|
local args, body = str:match([[^([%w,_ ]-)%->(.-)$]])
|
|
|
|
assert(args and body, "bad string lambda")
|
|
|
|
local s = "return function(" .. args .. ")\nreturn " .. body .. "\nend"
|
|
|
|
lambda_cache[str] = lume.dostring(s)
|
|
|
|
end
|
|
|
|
return lambda_cache[str]
|
2014-03-08 20:21:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-08-14 19:50:42 +00:00
|
|
|
local serialize
|
|
|
|
|
2015-08-15 10:31:50 +00:00
|
|
|
local serialize_map = {
|
2015-08-14 19:50:42 +00:00
|
|
|
[ "number" ] = tostring,
|
|
|
|
[ "boolean" ] = tostring,
|
|
|
|
[ "nil" ] = tostring,
|
|
|
|
[ "string" ] = function(v) return string.format("%q", v) end,
|
|
|
|
[ "table" ] = function(t, stk)
|
|
|
|
stk = stk or {}
|
|
|
|
if stk[t] then error("circular reference") end
|
2014-02-27 20:19:10 +00:00
|
|
|
local rtn = {}
|
2015-08-14 19:50:42 +00:00
|
|
|
stk[t] = true
|
2014-02-27 20:19:10 +00:00
|
|
|
for k, v in pairs(t) do
|
2015-08-14 19:50:42 +00:00
|
|
|
rtn[#rtn + 1] = "[" .. serialize(k, stk) .. "]=" .. serialize(v, stk)
|
2014-02-27 20:19:10 +00:00
|
|
|
end
|
2015-08-14 19:50:42 +00:00
|
|
|
stk[t] = nil
|
|
|
|
return "{" .. table.concat(rtn, ",") .. "}"
|
2014-02-27 20:19:10 +00:00
|
|
|
end
|
2015-08-14 19:50:42 +00:00
|
|
|
}
|
|
|
|
|
2015-08-15 10:31:50 +00:00
|
|
|
setmetatable(serialize_map, {
|
2015-08-14 19:50:42 +00:00
|
|
|
__index = function(t, k) error("unsupported serialize type: " .. k) end
|
|
|
|
})
|
|
|
|
|
|
|
|
serialize = function(x, stk)
|
2015-08-15 10:31:50 +00:00
|
|
|
return serialize_map[type(x)](x, stk)
|
2015-08-14 19:50:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function lume.serialize(x)
|
|
|
|
return serialize(x)
|
2014-02-27 20:19:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function lume.deserialize(str)
|
|
|
|
return lume.dostring("return " .. str)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function lume.split(str, sep)
|
2014-03-19 12:51:35 +00:00
|
|
|
if not sep then
|
|
|
|
return lume.array(str:gmatch("([%S]+)"))
|
|
|
|
else
|
|
|
|
assert(sep ~= "", "empty separator")
|
2014-03-19 21:05:34 +00:00
|
|
|
local psep = patternescape(sep)
|
|
|
|
return lume.array((str..sep):gmatch("(.-)("..psep..")"))
|
2014-03-19 12:51:35 +00:00
|
|
|
end
|
2014-02-27 20:19:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function lume.trim(str, chars)
|
2014-03-19 20:14:48 +00:00
|
|
|
if not chars then return str:match("^[%s]*(.-)[%s]*$") end
|
2014-03-19 21:05:34 +00:00
|
|
|
chars = patternescape(chars)
|
2014-02-27 20:19:10 +00:00
|
|
|
return str:match("^[" .. chars .. "]*(.-)[" .. chars .. "]*$")
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-01-10 20:26:19 +00:00
|
|
|
function lume.wordwrap(str, limit)
|
|
|
|
limit = limit or 72
|
|
|
|
local check
|
|
|
|
if type(limit) == "number" then
|
|
|
|
check = function(str) return #str >= limit end
|
|
|
|
else
|
|
|
|
check = limit
|
|
|
|
end
|
|
|
|
local rtn = {}
|
2015-05-05 18:12:41 +00:00
|
|
|
local line = ""
|
|
|
|
for word, spaces in str:gmatch("(%S+)(%s*)") do
|
|
|
|
local str = line .. word
|
|
|
|
if check(str) then
|
|
|
|
table.insert(rtn, line .. "\n")
|
|
|
|
line = word
|
|
|
|
else
|
|
|
|
line = str
|
|
|
|
end
|
|
|
|
for c in spaces:gmatch(".") do
|
|
|
|
if c == "\n" then
|
|
|
|
table.insert(rtn, line .. "\n")
|
|
|
|
line = ""
|
2015-01-10 20:26:19 +00:00
|
|
|
else
|
2015-05-05 18:12:41 +00:00
|
|
|
line = line .. c
|
2015-01-10 20:26:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-05-05 18:12:41 +00:00
|
|
|
table.insert(rtn, line)
|
|
|
|
return table.concat(rtn)
|
2015-01-10 20:26:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-02-27 20:19:10 +00:00
|
|
|
function lume.format(str, vars)
|
2014-03-03 21:08:19 +00:00
|
|
|
if not vars then return str end
|
2014-05-06 01:36:51 +00:00
|
|
|
local f = function(x)
|
|
|
|
return tostring(vars[x] or vars[tonumber(x)] or "{" .. x .. "}")
|
2014-03-04 12:18:50 +00:00
|
|
|
end
|
2014-02-27 20:19:10 +00:00
|
|
|
return (str:gsub("{(.-)}", f))
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-03-02 20:48:21 +00:00
|
|
|
function lume.trace(...)
|
|
|
|
local info = debug.getinfo(2, "Sl")
|
2015-08-14 19:27:31 +00:00
|
|
|
local t = { info.short_src .. ":" .. info.currentline .. ":" }
|
2014-04-18 18:09:59 +00:00
|
|
|
for i = 1, select("#", ...) do
|
|
|
|
local x = select(i, ...)
|
2015-01-12 19:51:24 +00:00
|
|
|
if type(x) == "number" then
|
2015-08-14 19:27:31 +00:00
|
|
|
x = tonumber(lume.round(x, .01))
|
2015-01-12 19:51:24 +00:00
|
|
|
end
|
2014-04-19 08:05:04 +00:00
|
|
|
t[#t + 1] = tostring(x)
|
2014-04-18 18:09:59 +00:00
|
|
|
end
|
|
|
|
print(table.concat(t, " "))
|
2014-03-02 20:48:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-02-27 20:19:10 +00:00
|
|
|
function lume.dostring(str)
|
2014-03-03 22:10:58 +00:00
|
|
|
return assert((loadstring or load)(str))()
|
2014-02-27 20:19:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-04-05 13:41:31 +00:00
|
|
|
function lume.uuid()
|
|
|
|
local fn = function(x)
|
2014-04-05 22:08:03 +00:00
|
|
|
local r = math_random(16) - 1
|
2014-04-05 13:41:31 +00:00
|
|
|
r = (x == "x") and (r + 1) or (r % 4) + 9
|
2014-04-05 22:08:03 +00:00
|
|
|
return ("0123456789abcdef"):sub(r, r)
|
2014-04-05 13:41:31 +00:00
|
|
|
end
|
|
|
|
return (("xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"):gsub("[xy]", fn))
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-03-01 15:05:51 +00:00
|
|
|
function lume.hotswap(modname)
|
2014-03-02 17:13:18 +00:00
|
|
|
local oldglobal = lume.clone(_G)
|
2014-03-01 15:05:51 +00:00
|
|
|
local updated = {}
|
|
|
|
local function update(old, new)
|
2014-05-06 01:36:51 +00:00
|
|
|
if updated[old] then return end
|
2014-03-01 15:05:51 +00:00
|
|
|
updated[old] = true
|
|
|
|
local oldmt, newmt = getmetatable(old), getmetatable(new)
|
|
|
|
if oldmt and newmt then update(oldmt, newmt) end
|
|
|
|
for k, v in pairs(new) do
|
|
|
|
if type(v) == "table" then update(old[k], v) else old[k] = v end
|
|
|
|
end
|
|
|
|
end
|
2014-03-02 17:13:18 +00:00
|
|
|
local err = nil
|
|
|
|
local function onerror(e)
|
|
|
|
for k, v in pairs(_G) do _G[k] = oldglobal[k] end
|
|
|
|
err = lume.trim(e)
|
2014-03-01 15:05:51 +00:00
|
|
|
end
|
2014-03-05 19:11:14 +00:00
|
|
|
local ok, oldmod = pcall(require, modname)
|
|
|
|
oldmod = ok and oldmod or nil
|
2014-03-02 17:13:18 +00:00
|
|
|
xpcall(function()
|
|
|
|
package.loaded[modname] = nil
|
|
|
|
local newmod = require(modname)
|
|
|
|
if type(oldmod) == "table" then update(oldmod, newmod) end
|
|
|
|
for k, v in pairs(oldglobal) do
|
2014-05-06 01:36:51 +00:00
|
|
|
if v ~= _G[k] and type(v) == "table" then
|
2014-03-02 17:13:18 +00:00
|
|
|
update(v, _G[k])
|
|
|
|
_G[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end, onerror)
|
|
|
|
package.loaded[modname] = oldmod
|
|
|
|
if err then return nil, err end
|
2014-03-01 15:05:51 +00:00
|
|
|
return oldmod
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-01-11 14:58:12 +00:00
|
|
|
local ripairs_iter = function(t, i)
|
|
|
|
i = i - 1
|
|
|
|
local v = t[i]
|
|
|
|
if v then return i, v end
|
|
|
|
end
|
|
|
|
|
|
|
|
function lume.ripairs(t)
|
|
|
|
return ripairs_iter, t, (#t + 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-05-09 14:12:52 +00:00
|
|
|
function lume.color(str, mul)
|
|
|
|
mul = mul or 1
|
|
|
|
local r, g, b, a
|
|
|
|
r, g, b = str:match("#(%x%x)(%x%x)(%x%x)")
|
|
|
|
if r then
|
|
|
|
r = tonumber(r, 16) / 0xff
|
|
|
|
g = tonumber(g, 16) / 0xff
|
|
|
|
b = tonumber(b, 16) / 0xff
|
|
|
|
a = 1
|
|
|
|
elseif str:match("rgba?%s*%([%d%s%.,]+%)") then
|
|
|
|
local f = str:gmatch("[%d.]+")
|
|
|
|
r = (f() or 0) / 0xff
|
|
|
|
g = (f() or 0) / 0xff
|
|
|
|
b = (f() or 0) / 0xff
|
|
|
|
a = f() or 1
|
|
|
|
else
|
|
|
|
error(("bad color string '%s'"):format(str))
|
|
|
|
end
|
|
|
|
return r * mul, g * mul, b * mul, a * mul
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-02-27 20:19:10 +00:00
|
|
|
function lume.rgba(color)
|
2014-05-06 01:41:02 +00:00
|
|
|
local a = math_floor((color / 16777216) % 256)
|
|
|
|
local r = math_floor((color / 65536) % 256)
|
|
|
|
local g = math_floor((color / 256) % 256)
|
2014-03-18 12:07:53 +00:00
|
|
|
local b = math_floor((color) % 256)
|
2014-02-27 20:19:10 +00:00
|
|
|
return r, g, b, a
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-04-05 15:30:10 +00:00
|
|
|
local chain_mt = {}
|
2014-04-17 11:13:50 +00:00
|
|
|
chain_mt.__index = lume.map(lume.filter(lume, iscallable, true),
|
2014-04-05 15:30:10 +00:00
|
|
|
function(fn)
|
|
|
|
return function(self, ...)
|
|
|
|
self._value = fn(self._value, ...)
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
chain_mt.__index.result = function(x) return x._value end
|
|
|
|
|
|
|
|
function lume.chain(value)
|
|
|
|
return setmetatable({ _value = value }, chain_mt)
|
|
|
|
end
|
|
|
|
|
2014-12-12 20:25:54 +00:00
|
|
|
setmetatable(lume, {
|
|
|
|
__call = function(t, ...)
|
|
|
|
return lume.chain(...)
|
|
|
|
end
|
|
|
|
})
|
|
|
|
|
2014-04-05 15:30:10 +00:00
|
|
|
|
2014-05-07 07:21:36 +00:00
|
|
|
return lume
|