mirror of
https://github.com/TangentFoxy/lume.git
synced 2025-07-29 11:32:20 +00:00
Compare commits
27 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
a234e36975 | ||
|
4ba373c2d1 | ||
|
c537d00a47 | ||
|
2c3be23a2b | ||
|
be40cd6276 | ||
|
c971cee753 | ||
|
151b57adc6 | ||
|
cbafc49e8a | ||
|
fcb1fa90d4 | ||
|
2a6d1ea9a8 | ||
|
2d7fa98155 | ||
|
af4c919be4 | ||
|
abdfd78354 | ||
|
fbb5cbac1a | ||
|
f8cbfc0bb0 | ||
|
cf031025fd | ||
|
4db5a6b683 | ||
|
a4fe134985 | ||
|
cbff46bdfb | ||
|
5e15a57f37 | ||
|
2f388bc2c7 | ||
|
8f4d1b158f | ||
|
1bbb795a89 | ||
|
2c71079ef3 | ||
|
8a76fd7595 | ||
|
b4bea5f4e0 | ||
|
51189d190d |
36
README.md
36
README.md
@@ -64,6 +64,16 @@ raised.
|
|||||||
lume.randomchoice({true, false}) -- Returns either true or false
|
lume.randomchoice({true, false}) -- Returns either true or false
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### lume.weightedchoice(t)
|
||||||
|
Takes the argument table `t` where the keys are the possible choices and the
|
||||||
|
value is the choice's weight. A weight should be 0 or above, the larger the
|
||||||
|
number the higher the probability of that choice being picked. If the table is
|
||||||
|
empty, a weight is below zero or all the weights are 0 then an error is raised.
|
||||||
|
```lua
|
||||||
|
lume.weightedchoice({ ["cat"] = 10, ["dog"] = 5, ["frog"] = 0 })
|
||||||
|
-- Returns either "cat" or "dog" with "cat" being twice as likely to be chosen.
|
||||||
|
```
|
||||||
|
|
||||||
### lume.shuffle(t)
|
### lume.shuffle(t)
|
||||||
Shuffles the values of array `t` in place, returns the array.
|
Shuffles the values of array `t` in place, returns the array.
|
||||||
|
|
||||||
@@ -105,12 +115,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.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
|
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
|
left to right, so as to reduce the array to a single value. If a `first` value
|
||||||
intialised to the `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
|
```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])
|
### lume.set(t [, retainkeys])
|
||||||
@@ -151,6 +163,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.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)
|
### lume.clone(t)
|
||||||
Returns a shallow copy of the table `t`.
|
Returns a shallow copy of the table `t`.
|
||||||
|
|
||||||
@@ -181,6 +200,15 @@ 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.lambda(str)
|
||||||
|
Takes a string lambda and returns a function. `str` should be a list of
|
||||||
|
comma-seperated parameters, followed by `->`, followed by the expression which
|
||||||
|
will be evaluated and returned.
|
||||||
|
```lua
|
||||||
|
local f = lume.lambda "x,y -> 2*x+y"
|
||||||
|
f(10, 5) -- Returns 25
|
||||||
|
```
|
||||||
|
|
||||||
### lume.serialize(x)
|
### lume.serialize(x)
|
||||||
Serializes the argument `x` into a string which can be loaded again using
|
Serializes the argument `x` into a string which can be loaded again using
|
||||||
`lume.deserialize()`. Only booleans, numbers, tables and strings can be
|
`lume.deserialize()`. Only booleans, numbers, tables and strings can be
|
||||||
|
97
lume.lua
97
lume.lua
@@ -7,7 +7,21 @@
|
|||||||
-- 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.1.1" }
|
local lume = { _version = "1.2.0" }
|
||||||
|
|
||||||
|
local pairs, ipairs = pairs, ipairs
|
||||||
|
local type, assert, unpack = type, assert, unpack
|
||||||
|
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
|
||||||
|
local math_atan2 = math.atan2
|
||||||
|
local math_sqrt = math.sqrt
|
||||||
|
local math_abs = math.abs
|
||||||
|
local math_min = math.min
|
||||||
|
local math_max = math.max
|
||||||
|
local math_pi = math.pi
|
||||||
|
|
||||||
|
|
||||||
function lume.clamp(x, min, max)
|
function lume.clamp(x, min, max)
|
||||||
@@ -17,7 +31,7 @@ end
|
|||||||
|
|
||||||
function lume.round(x, increment)
|
function lume.round(x, increment)
|
||||||
if increment then return lume.round(x / increment) * increment end
|
if increment then return lume.round(x / increment) * increment end
|
||||||
return x > 0 and math.floor(x + .5) or math.ceil(x - .5)
|
return x > 0 and math_floor(x + .5) or math_ceil(x - .5)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -32,13 +46,13 @@ 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 m = (1 - math_cos(lume.clamp(amount, 0, 1) * math_pi)) / 2
|
||||||
return a + (b - a) * m
|
return a + (b - a) * m
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function lume.pingpong(x)
|
function lume.pingpong(x)
|
||||||
return 1 - math.abs(1 - x % 2)
|
return 1 - math_abs(1 - x % 2)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -46,30 +60,45 @@ function lume.distance(x1, y1, x2, y2, squared)
|
|||||||
local dx = x1 - x2
|
local dx = x1 - x2
|
||||||
local dy = y1 - y2
|
local dy = y1 - y2
|
||||||
local s = dx * dx + dy * dy
|
local s = dx * dx + dy * dy
|
||||||
return squared and s or math.sqrt(s)
|
return squared and s or math_sqrt(s)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function lume.angle(x1, y1, x2, y2)
|
function lume.angle(x1, y1, x2, y2)
|
||||||
return math.atan2(y2 - y1, x2 - x1)
|
return math_atan2(y2 - y1, x2 - x1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function lume.random(a, b)
|
function lume.random(a, b)
|
||||||
if not a then a, b = 0, 1 end
|
if not a then a, b = 0, 1 end
|
||||||
if not b then b = 0 end
|
if not b then b = 0 end
|
||||||
return a + math.random() * (b - a)
|
return a + math_random() * (b - a)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function lume.randomchoice(t)
|
function lume.randomchoice(t)
|
||||||
return t[math.random(#t)]
|
return t[math_random(#t)]
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
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
|
end
|
||||||
|
|
||||||
|
|
||||||
function lume.shuffle(t)
|
function lume.shuffle(t)
|
||||||
for i = 1, #t do
|
for i = 1, #t do
|
||||||
local r = math.random(#t)
|
local r = math_random(#t)
|
||||||
t[i], t[r] = t[r], t[i]
|
t[i], t[r] = t[r], t[i]
|
||||||
end
|
end
|
||||||
return t
|
return t
|
||||||
@@ -119,16 +148,18 @@ end
|
|||||||
|
|
||||||
|
|
||||||
function lume.reduce(t, fn, first)
|
function lume.reduce(t, fn, first)
|
||||||
for i = 1, #t do first = fn(first, t[i]) end
|
local acc = first or t[1]
|
||||||
return first
|
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
|
end
|
||||||
|
|
||||||
|
|
||||||
function lume.set(t, retainkeys)
|
function lume.set(t, retainkeys)
|
||||||
local tmp = {}
|
|
||||||
for k, v in pairs(t) do tmp[v] = k end
|
|
||||||
local rtn = {}
|
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
|
return rtn
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -159,16 +190,24 @@ end
|
|||||||
|
|
||||||
|
|
||||||
function lume.slice(t, i, j)
|
function lume.slice(t, i, j)
|
||||||
i = i or 1
|
local function index(x) return x < 0 and (#t + x + 1) or x end
|
||||||
j = j and (j < 0 and (#t + j + 1) or j) or #t
|
i = i and index(i) or 1
|
||||||
|
j = j and index(j) or #t
|
||||||
local rtn = {}
|
local rtn = {}
|
||||||
for i = math.max(i, 1), math.min(j, #t) do
|
for i = math_max(i, 1), math_min(j, #t) do
|
||||||
rtn[#rtn + 1] = t[i]
|
rtn[#rtn + 1] = t[i]
|
||||||
end
|
end
|
||||||
return rtn
|
return rtn
|
||||||
end
|
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)
|
function lume.clone(t)
|
||||||
local rtn = {}
|
local rtn = {}
|
||||||
for k, v in pairs(t) do rtn[k] = v end
|
for k, v in pairs(t) do rtn[k] = v end
|
||||||
@@ -203,6 +242,19 @@ function lume.time(fn, ...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local lambda_cache = {}
|
||||||
|
|
||||||
|
function lume.lambda(str)
|
||||||
|
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]
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function lume.serialize(x)
|
function lume.serialize(x)
|
||||||
local f = { string = function(v) return string.format("%q", v) end,
|
local f = { string = function(v) return string.format("%q", v) end,
|
||||||
number = tostring, boolean = tostring }
|
number = tostring, boolean = tostring }
|
||||||
@@ -213,6 +265,8 @@ function lume.serialize(x)
|
|||||||
end
|
end
|
||||||
return "{" .. table.concat(rtn) .. "}"
|
return "{" .. table.concat(rtn) .. "}"
|
||||||
end
|
end
|
||||||
|
local err = function(t,k) error("unsupported serialize type: " .. k) end
|
||||||
|
setmetatable(f, { __index = err })
|
||||||
return f[type(x)](x)
|
return f[type(x)](x)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -291,11 +345,10 @@ end
|
|||||||
|
|
||||||
|
|
||||||
function lume.rgba(color)
|
function lume.rgba(color)
|
||||||
local floor = math.floor
|
local a = math_floor((color / 2 ^ 24) % 256)
|
||||||
local a = floor((color / 2 ^ 24) % 256)
|
local r = math_floor((color / 2 ^ 16) % 256)
|
||||||
local r = floor((color / 2 ^ 16) % 256)
|
local g = math_floor((color / 2 ^ 08) % 256)
|
||||||
local g = floor((color / 2 ^ 08) % 256)
|
local b = math_floor((color) % 256)
|
||||||
local b = floor((color) % 256)
|
|
||||||
return r, g, b, a
|
return r, g, b, a
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@@ -82,6 +82,9 @@ end
|
|||||||
|
|
||||||
-- lume.random
|
-- lume.random
|
||||||
tests["lume.random"] = function()
|
tests["lume.random"] = function()
|
||||||
|
testeq( type(lume.random()), "number" )
|
||||||
|
testeq( type(lume.random(1)), "number" )
|
||||||
|
testeq( type(lume.random(1, 2)), "number" )
|
||||||
end
|
end
|
||||||
|
|
||||||
-- lume.randomchoice
|
-- lume.randomchoice
|
||||||
@@ -94,6 +97,15 @@ tests["lume.randomchoice"] = function()
|
|||||||
testeq( lume.randomchoice({true}), true )
|
testeq( lume.randomchoice({true}), true )
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- lume.weightedchoice
|
||||||
|
tests["lume.weightedchoice"] = function()
|
||||||
|
testeq( lume.weightedchoice( {a = 1} ), "a" )
|
||||||
|
testeq( lume.weightedchoice( {a = 0, b = 1} ), "b" )
|
||||||
|
tester.test.error( lume.weightedchoice, {} )
|
||||||
|
tester.test.error( lume.weightedchoice, { a = 0, b = 0 } )
|
||||||
|
tester.test.error( lume.weightedchoice, { a = 1, b = -1 } )
|
||||||
|
end
|
||||||
|
|
||||||
-- lume.shuffle
|
-- lume.shuffle
|
||||||
tests["lume.shuffle"] = function()
|
tests["lume.shuffle"] = function()
|
||||||
local t = {1, 2, 3, 4, 5}
|
local t = {1, 2, 3, 4, 5}
|
||||||
@@ -150,12 +162,19 @@ end
|
|||||||
-- lume.reduce
|
-- lume.reduce
|
||||||
tests["lume.reduce"] = function()
|
tests["lume.reduce"] = function()
|
||||||
local concat = function(a, b) return a .. b end
|
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, ""), "catdog" )
|
||||||
testeq( lume.reduce({"cat", "dog"}, concat, "pig"), "pigcatdog" )
|
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, 5), 15 )
|
||||||
|
testeq( lume.reduce({1}, add), 1 )
|
||||||
|
testeq( lume.reduce({}, concat, "potato"), "potato" )
|
||||||
end
|
end
|
||||||
|
|
||||||
-- lume.set
|
-- lume.set
|
||||||
tests["lume.set"] = function()
|
tests["lume.set"] = function()
|
||||||
|
testeq( lume.set({}), {} )
|
||||||
local t = lume.set({1, 2, 3, 2, 5, 6, 6})
|
local t = lume.set({1, 2, 3, 2, 5, 6, 6})
|
||||||
table.sort(t)
|
table.sort(t)
|
||||||
testeq( t, {1, 2, 3, 5, 6} )
|
testeq( t, {1, 2, 3, 5, 6} )
|
||||||
@@ -194,6 +213,19 @@ tests["lume.slice"] = function()
|
|||||||
testeq( lume.slice({"a", "b", "c", "d", "e"}, 4), {"d", "e"} )
|
testeq( lume.slice({"a", "b", "c", "d", "e"}, 4), {"d", "e"} )
|
||||||
testeq( lume.slice({"a", "b", "c", "d", "e"}, 1, 1), {"a"} )
|
testeq( lume.slice({"a", "b", "c", "d", "e"}, 1, 1), {"a"} )
|
||||||
testeq( lume.slice({"a", "b", "c", "d", "e"}, 2, 1), {} )
|
testeq( lume.slice({"a", "b", "c", "d", "e"}, 2, 1), {} )
|
||||||
|
testeq( lume.slice({"a", "b", "c", "d", "e"}, -3, -2), {"c", "d"} )
|
||||||
|
testeq( lume.slice({"a", "b", "c", "d", "e"}, -3, 1), {} )
|
||||||
|
testeq( lume.slice({"a", "b", "c", "d", "e"}, 0, 1), {"a"} )
|
||||||
|
testeq( lume.slice({"a", "b", "c", "d", "e"}, 0, 0), {} )
|
||||||
|
testeq( lume.slice({"a", "b", "c", "d", "e"}, -3), {"c", "d", "e"} )
|
||||||
|
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
|
end
|
||||||
|
|
||||||
-- lume.clone
|
-- lume.clone
|
||||||
@@ -224,6 +256,21 @@ tests["lume.time"] = function()
|
|||||||
testeq( {a, b, c}, {50, 60, 70} )
|
testeq( {a, b, c}, {50, 60, 70} )
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- lume.lambda
|
||||||
|
tests["lume.lambda"] = function()
|
||||||
|
testeq( lume.lambda "x->x*x"(10), 100 )
|
||||||
|
testeq( lume.lambda "x->x*x"(20), 400 )
|
||||||
|
testeq( lume.lambda "x,y -> 2*x+y"(10,5), 25 )
|
||||||
|
testeq( lume.lambda "a, b -> a / b"(1, 2), .5 )
|
||||||
|
testeq( lume.lambda "a -> 'hi->' .. a"("doggy"), "hi->doggy" )
|
||||||
|
testeq( lume.lambda "A1,_->A1.._"("te","st"), "test" )
|
||||||
|
testeq( lume.lambda "->"(1,2,3), nil )
|
||||||
|
tester.test.error( lume.lambda, "abc" )
|
||||||
|
tester.test.error( lume.lambda, "" )
|
||||||
|
tester.test.error( lume.lambda, "a,b->a->b" )
|
||||||
|
tester.test.error( lume.lambda, "(a),b->a+b" )
|
||||||
|
end
|
||||||
|
|
||||||
-- lume.serialize / lume.deserialize
|
-- lume.serialize / lume.deserialize
|
||||||
tests["lume.serialize, lume.deserialize"] = function()
|
tests["lume.serialize, lume.deserialize"] = function()
|
||||||
local t = { 1, 2, 3, 4, true, false, "cat", "dog", {1, 2, 3} }
|
local t = { 1, 2, 3, 4, true, false, "cat", "dog", {1, 2, 3} }
|
||||||
|
@@ -128,6 +128,18 @@ function tester.test.equal(result, expected)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function tester.test.error(fn, ...)
|
||||||
|
local passed = not pcall(fn, ...)
|
||||||
|
local info = debug.getinfo(2)
|
||||||
|
if passed then
|
||||||
|
dopass(info.short_src, info.currentline)
|
||||||
|
else
|
||||||
|
dofail(info.short_src, info.currentline)
|
||||||
|
printfailmsg("Expected an error to be raised")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function tester.dotests(t)
|
function tester.dotests(t)
|
||||||
local keys = {}
|
local keys = {}
|
||||||
for k in pairs(t) do table.insert(keys, k) end
|
for k in pairs(t) do table.insert(keys, k) end
|
||||||
|
Reference in New Issue
Block a user