Returns the number
valuein its new range.
Returns the number
valuein its new range. This function doesn't clamp the value to the new range so it can be used to extrapolate values. It also doesn't catch a division by zero error so make sure thatstart1andstop1are not the same value.
Compares whether two values are within a range. Useful for fuzzy comparisons: whether values are approximately equal.
lume.approximately(2.34567, 2.3, 0.001) -- Returns false
lume.approximately(2.34567, 2.3, 0.1) -- Returns true
lume.approximately(0, 0.1, 0.001) -- Returns falseReturns the number
xclamped between the numbersminandmax
Returns the linearly interpolated number between
aandb,amountshould be in the range of 0 - 1; ifamountis outside of this range it is clamped.
lume.lerp(100, 200, 0.5) -- Returns 150Similar to
lume.lerp()but uses cubic interpolation instead of linear interpolation.
Returns the distance between the two points. If
squaredis true then the squared distance is returned.
This is faster to calculate and can still be used when comparing distances.
Rounds
xto the nearest integer; rounds away from zero if we're midway between two integers.
Ifincrementis set then the number is rounded to the nearest increment.
lume.round(2.3) -- Returns 2
lume.round(123.4567, 0.1) -- Returns 123.5Returns
1ifxis 0 or above, returns-1whenxis negative.
Returns the angle between the two points.
Returns true if all the values in
ttable are true. If afnfunction is supplied it is called on each value, true is returned if all of the calls tofnreturn true.
lume.all({1, 2, 1}, function(x) return x == 1 end) -- Returns falseReturns true if any of the values in
ttable is true. If afnfunction is supplied it is called on each value, true is returned if any of the calls tofnreturns true.
lume.any({1, 2, 1}, function(x) return x == 1 end) -- Returns trueReturns
trueifxis an array -- the value is assumed to be an array if it is a table which contains a value at the index1.
Compares
#twithlume.count(t)and returnstrue, #tif they are equal andfalse, lume.count(t), #tif not.
Pushes all the given values to the end of the table
tand returns the pushed values. Nil values are ignored.
local t = { 1, 2, 3 }
lume.push(t, 4, 5) -- `t` becomes { 1, 2, 3, 4, 5 }Pops off the last value in the table
t(if its an array) and returns the value
local t = { 1, 2, 3 }
lume.pop(t) -- returns 3 and t becomes { 1, 2 }Removes the first instance of the value
xif it exists in the tablet.
Returnsx.
local t = { 1, 2, 3 }
lume.remove(t, 2) -- `t` becomes { 1, 3 }Stable remove from list-like table. Fast for removing many elements. Doesn't change order of elements. See reference.
local t = { 1, 2, 3 }
lume.removeall(t, function(x, i, j) return x == 1 end) -- `t` becomes {2, 3}Unstable remove from list-like table.
Fast for removing a few elements, but modifies order. See reference.
local t = { 1, 2, 3 }
lume.removeswap(t, function(x) return x == 1 end) -- `t` becomes {3, 2}Nils all the values in the table
t, this renders the table empty.
Returnst.
local t = { 1, 2, 3 }
lume.clear(t) -- `t` becomes {}Returns the index/key of
valueint. Returnsnilif that value does not exist in the table.
lume.find({'a', 'b', 'c'}, 'b') -- Returns 2Returns the value and key of the value in table
twhich returns true whenfnis called on it. Returnsnilif no such value exists.
lume.match({1, 5, 8, 7}, function(x) return x % 2 == 0 end) -- Returns 8, 3Iterates the table
tand calls the functionfnon each value followed by the supplied additional arguments.
Iffnis a string the method of that name is called for each value. The function returnstunmodified.
lume.each({1, 2, 3}, print) -- Prints '1', '2', '3' on separate lines
lume.each({a, b, c}, 'move', 10, 20) -- Does x:move(10, 20) on each valueApplies the function
fnto each value in tabletand returns a new table with the resulting values.
lume.map({1, 2, 3}, function(x) return x * 2 end) -- Returns {2, 4, 6}Calls
fnon each value ofttable.
Returns a new table with only the values wherefnreturned true.
Ifretainkeysis true the table is not treated as an array and retains its original keys.
lume.filter({1, 2, 3, 4}, function(x) return x % 2 == 0 end) -- Returns {2, 4}The opposite of
lume.filter().
Instead it returns a new table with only the values wherefnreturned false.
Ifretainkeysis true the table is not treated as an array and retains its original keys.
lume.reject({1, 2, 3, 4}, function(x) return x % 2 == 0 end) -- Returns {1, 3}Returns a copy of the
tarray with all the duplicate values removed.
lume.unique({2, 1, 2, 'cat', 'cat'}) -- Returns {1, 2, 'cat'}Returns a copy of the table filtered to only contain values for the given keys.
lume.pick({ a = 1, b = 2, c = 3 }, 'a', 'c') -- Returns { a = 1, c = 3 }Applies
fnon two arguments cumulative to the items of the arrayt, from left to right, so as to reduce the array to a single value.
If afirstvalue is specified the accumulator is initialised to this, otherwise the first value in the array is used.
If the array is empty and nofirstvalue is specified an error is raised.
lume.reduce({1, 2, 3}, function(a, b) return a + b end) -- Returns 6Copies all the fields from the source tables to the table
tand returnst.
If a key exists in multiple tables the right-most table's value is used.
local t = { a = 1, b = 2 }
lume.extend(t, { b = 4, c = 6 }) -- `t` becomes { a = 1, b = 4, c = 6 }Returns a new table with all the given tables merged together.
If a key exists in multiple tables the right-most table's value is used.
lume.merge({a=1, b=2, c=3}, {c=8, d=9}) -- Returns {a=1, b=2, c=8, d=9}Returns a new array consisting of all the given arrays concatenated into one.
lume.concat({1, 2}, {3, 4}, {5, 6}) -- Returns {1, 2, 3, 4, 5, 6}Returns a wrapped object which allows chaining of lume functions.
The function result() should be called at the end of the chain to return the resulting value.
lume.chain({1, 2, 3, 4})
:filter(function(x) return x % 2 == 0 end)
:map(function(x) return -x end)
:result() -- Returns { -2, -4 }The table returned by the
lumemodule, when called, acts the same as callinglume.chain().
lume({1, 2, 3}):each(print) -- Prints 1, 2 then 3 on separate linesCounts the number of values in the table
t.
If afnfunction is supplied it is called on each value, the number of times it returns true is counted.
lume.count({a = 2, b = 3, c = 4, d = 5}) -- Returns 4
lume.count({1, 2, 4, 6}, function(x) return x % 2 == 0 end) -- Returns 3Returns the depth of
t
lume.depth({{{{a=1}}}}) -- Returns 4Returns the first element of an array or nil if the array is empty.
Ifnis specificed an array of the firstnelements is returned.
lume.first({'a', 'b', 'c'}) -- Returns 'a'Returns the last element of an array or nil if the array is empty.
Ifnis specificed an array of the lastnelements is returned.
lume.last({'a', 'b', 'c'}) -- Returns 'c'Returns the highest value of an array or
nilif the array is empty.
Returns the lowest value of an array or
nilif the array is empty.
Returns an array containing each key of the table.
Returns a shallow copy of the table
t.
Returns a deep copy of the table
t.
Mimics the behaviour of Lua's
string.sub, but operates on an array rather 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'}Returns a copy of the table where the keys have become the values and the values the keys.
lume.invert({a = 'x', b = 'y'}) -- returns {x = 'a', y = 'b'}Iterates the supplied iterator and returns an array filled with the values.
lume.array(string.gmatch('Hello world', '%a+')) -- Returns {'Hello', 'world'}Returns a shuffled copy of the array
t.
Returns a copy of the array
twith all its items sorted.
Ifcompis a function it will be used to compare the items when sorting.
Ifcompis a string it will be used as the key to sort the items by.
lume.sort({ 1, 4, 3, 2, 5 }) -- Returns { 1, 2, 3, 4, 5 }
lume.sort({ {z=2}, {z=3}, {z=1} }, 'z') -- Returns { {z=1}, {z=2}, {z=3} }
lume.sort({ 1, 3, 2 }, function(a, b) return a > b end) -- Returns { 3, 2, 1 }Returns a wrapper function to
fnwhere the results for any given set of arguments are cached.
It is useful when used on functions with slow-running computations.
fib = lume.memoize(function(n) return n < 2 and n or fib(n-1) + fib(n-2) end)Returns a wrapper function to
fnwhich takes the supplied arguments.
The wrapper function will callfnon the first call and do nothing on any subsequent calls.
local f = lume.once(print, 'Hello')
f() -- Prints 'Hello'
f() -- Does nothingTakes a string lambda and returns a function.
strshould be a list of comma-separated parameters, followed by->, followed by the expression which will be evaluated and returned.
local f = lume.lambda'x,y -> 2*x+y' -- or lume.l'x,y -> @*x*y'
f(10, 5) -- Returns 25Creates a wrapper function which calls each supplied argument in the order they were passed in.
nilarguments are ignore. The wrapper function passes its own arguments to each of its wrapped functions when it is called.
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 lineCalls the given function with the provided arguments and returns its values. If
fnisnilthen no action is performed and the function returnsnil.
lume.call(print, 'Hello world') -- Prints 'Hello world'Creates a wrapper function around function
fn, automatically inserting the arguments intofnwhich will persist every time the wrapper is called. Any arguments which are passed to the returned function will be inserted after the already existing arguments passed tofn.
local f = lume.fn(print, 'Hello')
f('world') -- Prints 'Hello world'Returns an array of the words in the string
str. Ifsepis provided it is used as the delimiter, consecutive delimiters are not grouped together and will delimit empty strings.
lume.split('One two three') -- Returns {'One', 'two', 'three'}
lume.split('a,b,,c', ',') -- Returns {'a', 'b', '', 'c'}Returns a formatted string. The values of keys in the table
varscan be inserted into the string by using the form'{key}'instr; numerical keys can also be used.
lume.format('{b} hi {a}', {a = 'mark', b = 'Oh'}) -- Returns 'Oh hi mark'
lume.format('Hello {1}!', {'world'}) -- Returns 'Hello world!'Trims the whitespace from the start and end of the string
strand returns the new string.
If acharsvalue is set the characters incharsare trimmed instead of whitespace.
lume.trim(' Hello ') -- Returns 'Hello'Returns
strwrapped tolimitnumber of characters per line, by defaultlimitis72.limitcan also be a function which when passed a string, returnstrueif it is too long for a single line.
-- Returns 'Hello world\nThis is a\nshort string'
lume.wordwrap('Hello world. This is a short string', 14)Inserts the arguments into function
fnand calls it.
Returns the time in seconds the functionfntook to execute followed byfn's returned values.
lume.time(function(x) return x end, 'hello') -- Returns 0, 'hello'Performs the same function as
ipairs()but iterates in reverse.
This allows the removal of items from the table during iteration without any items being skipped.
-- Prints '3->c', '2->b' and '1->a' on separate lines
for i, v in lume.ripairs({ 'a', 'b', 'c' }) do
print(i .. '->' .. v)
endExecutes the lua code inside
str.
lume.dostring('print("Hello!")') -- Prints 'Hello!'Reloads an already loaded module in place, allowing you to immediately see the effects of code changes without having to restart the program.
modnameshould be the same string used when loading the module with require().
In the case of an error the global environment is restored andnilplus an error message is returned.
lume.hotswap('lume') -- Reloads the lume module
assert(lume.hotswap('inexistant_module')) -- Raises an errorPrints the current filename and line number followed by each argument separated by a space.
-- Assuming the file is called 'example.lua' and the next line is 12:
lume.trace('hello', 1234) -- Prints 'example.lua:12: hello 1234'Takes color string
strand returns 4 values, one for each color channel (r,g,banda).
By default the returned values are between 0 and 1; the values are multiplied by the numbermulif it is provided.
lume.color('#ff0000') -- Returns 1, 0, 0, 1
lume.color('rgba(255, 0, 255, .5)') -- Returns 1, 0, 1, .5
lume.color('#00ffff', 256) -- Returns 0, 256, 256, 256
lume.color('rgb(255, 0, 0)', 256) -- Returns 256, 0, 0, 256Generates a random UUID string; version 4 as specified in RFC 4122.
Returns a random floating-point number between
aandb. Unlike math.random, passing two integers will not return an integer.
With both args, returns a number in the range
[a,b]. If onlyais supplied, returns a number in the range[0,a]. If no arguments are supplied, returns a number in the range[0,1].
Returns a random value from array
t.
If the array is empty an error is raised.
lume.randomchoice({true, false}) -- Returns either true or falseTakes the argument table
twhere 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.
lume.weightedchoice({ ['cat'] = 10, ['dog'] = 5, ['frog'] = 0 })
-- Returns either 'cat' or 'dog' with 'cat' being twice as likely to be chosen.Several lume functions allow a
table,stringornilto be used in place of their iteratee function argument. The functions that provide this behaviour are:map(),all(),any(),filter(),reject(),match()andcount().
If the argument is
nilthen each value will return itself.
lume.filter({ true, true, false, true }, nil) -- { true, true, true }If the argument is a
stringthen each value will be assumed to be a table, and will return the value of the key which matches the string.
local t = {{ z = 'cat' }, { z = 'dog' }, { z = 'owl' }}
lume.map(t, 'z') -- Returns { 'cat', 'dog', 'owl' }If the argument is a
tablethen each value will returntrueorfalse, depending on whether the values at each of the table's keys match the collection's value's values.
local t = {
{ age = 10, type = 'cat' },
{ age = 8, type = 'dog' },
{ age = 10, type = 'owl' },
}
lume.count(t, { age = 10 }) -- returns 2