2014-03-03 12:11:57 +00:00
|
|
|
# Lume
|
2014-02-27 20:19:10 +00:00
|
|
|
|
2014-03-01 15:34:40 +00:00
|
|
|
A collection of functions for Lua, geared towards game development.
|
2014-02-27 20:19:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
2014-03-02 17:13:18 +00:00
|
|
|
The [lume.lua](lume.lua?raw=1) file should be dropped into an existing project
|
|
|
|
and required by it:
|
2014-02-27 20:19:10 +00:00
|
|
|
|
|
|
|
```lua
|
|
|
|
lume = require "lume"
|
|
|
|
```
|
|
|
|
|
|
|
|
|
2014-03-03 12:11:57 +00:00
|
|
|
## Function Reference
|
2014-02-27 20:19:10 +00:00
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.clamp(x, min, max)
|
2014-03-03 12:11:57 +00:00
|
|
|
Returns the number `x` clamped between the numbers `min` and `max`
|
2014-02-27 20:19:10 +00:00
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.round(x [, increment])
|
2014-03-02 21:29:23 +00:00
|
|
|
Rounds `x` to the nearest integer; rounds away from zero if we're midway
|
|
|
|
between two integers. If `increment` is set then the number is rounded to the
|
|
|
|
nearest increment.
|
|
|
|
```lua
|
|
|
|
lume.round(2.3) -- Returns 2
|
|
|
|
lume.round(123.4567, .1) -- Returns 123.5
|
|
|
|
```
|
2014-02-27 20:19:10 +00:00
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.sign(x)
|
2014-02-27 20:19:10 +00:00
|
|
|
Returns `1` if `x` is 0 or above, returns `-1` when `x` is negative.
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.lerp(a, b, amount)
|
2014-03-03 12:11:57 +00:00
|
|
|
Returns the linearly interpolated number between `a` and `b`, `amount` should
|
|
|
|
be in the range of 0 - 1; if `amount` is outside of this range it is clamped.
|
2014-03-02 20:59:32 +00:00
|
|
|
```lua
|
|
|
|
lume.lerp(100, 200, .5) -- Returns 150
|
|
|
|
```
|
2014-02-27 20:19:10 +00:00
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.smooth(a, b, amount)
|
2014-05-06 01:39:53 +00:00
|
|
|
Similar to `lume.lerp()` but uses cubic interpolation instead of linear
|
2014-02-27 21:11:31 +00:00
|
|
|
interpolation.
|
2014-02-27 20:19:10 +00:00
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.pingpong(x)
|
2014-03-03 12:11:57 +00:00
|
|
|
Ping-pongs the number `x` between 0 and 1.
|
2014-02-27 20:19:10 +00:00
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.distance(x1, y1, x2, y2 [, squared])
|
2014-03-04 22:53:31 +00:00
|
|
|
Returns the distance between the two points. If `squared` is true then the
|
|
|
|
squared distance is returned -- this is faster to calculate and can still be
|
|
|
|
used when comparing distances.
|
2014-02-27 20:19:10 +00:00
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.angle(x1, y1, x2, y2)
|
2014-02-27 20:19:10 +00:00
|
|
|
Returns the angle between the two points.
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.vector(angle, magnitude)
|
2016-09-19 20:55:59 +00:00
|
|
|
Given an `angle` and `magnitude`, returns a vector.
|
|
|
|
```lua
|
|
|
|
local x, y = lume.vector(0, 10) -- Returns 10, 0
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.random([a [, b]])
|
2014-02-27 23:40:40 +00:00
|
|
|
Returns a random number between `a` and `b`. If only `a` is supplied a number
|
2014-02-27 20:19:10 +00:00
|
|
|
between `0` and `a` is returned. If no arguments are supplied a random number
|
|
|
|
between `0` and `1` is returned.
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.randomchoice(t)
|
2014-03-04 21:21:50 +00:00
|
|
|
Returns a random value from array `t`. If the array is empty an error is
|
|
|
|
raised.
|
2014-03-02 20:59:32 +00:00
|
|
|
```lua
|
|
|
|
lume.randomchoice({true, false}) -- Returns either true or false
|
|
|
|
```
|
2014-02-27 20:19:10 +00:00
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.weightedchoice(t)
|
2014-03-12 13:12:45 +00:00
|
|
|
Takes the argument table `t` where the keys are the possible choices and the
|
2014-03-12 13:16:14 +00:00
|
|
|
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.
|
2014-03-12 13:12:45 +00:00
|
|
|
```lua
|
|
|
|
lume.weightedchoice({ ["cat"] = 10, ["dog"] = 5, ["frog"] = 0 })
|
|
|
|
-- Returns either "cat" or "dog" with "cat" being twice as likely to be chosen.
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.isarray(x)
|
2016-02-21 16:43:18 +00:00
|
|
|
Returns `true` if `x` is an array -- the value is assumed to be an array if it
|
|
|
|
is a table which contains a value at the index `1`. This function is used
|
|
|
|
internally and can be overridden if you wish to use a different method to detect
|
|
|
|
arrays.
|
|
|
|
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.push(t, ...)
|
2015-02-07 23:04:23 +00:00
|
|
|
Pushes all the given values to the end of the table `t` and returns the pushed
|
|
|
|
values. Nil values are ignored.
|
2015-02-07 22:56:13 +00:00
|
|
|
```lua
|
|
|
|
local t = { 1, 2, 3 }
|
|
|
|
lume.push(t, 4, 5) -- `t` becomes { 1, 2, 3, 4, 5 }
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.remove(t, x)
|
2015-02-07 22:56:13 +00:00
|
|
|
Removes the first instance of the value `x` if it exists in the table `t`.
|
|
|
|
Returns `x`.
|
|
|
|
```lua
|
|
|
|
local t = { 1, 2, 3 }
|
|
|
|
lume.remove(t, 2) -- `t` becomes { 1, 3 }
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.clear(t)
|
2015-02-08 15:12:57 +00:00
|
|
|
Nils all the values in the table `t`, this renders the table empty. Returns
|
|
|
|
`t`.
|
2015-02-07 22:56:13 +00:00
|
|
|
```lua
|
|
|
|
local t = { 1, 2, 3 }
|
|
|
|
lume.clear(t) -- `t` becomes {}
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.extend(t, ...)
|
2015-05-05 18:21:56 +00:00
|
|
|
Copies all the fields from the source tables to the table `t` and returns `t`.
|
2015-05-09 13:27:38 +00:00
|
|
|
If a key exists in multiple tables the right-most table's value is used.
|
2015-05-05 18:37:52 +00:00
|
|
|
```lua
|
2015-05-05 18:21:56 +00:00
|
|
|
local t = { a = 1, b = 2 }
|
|
|
|
lume.extend(t, { b = 4, c = 6 }) -- `t` becomes { a = 1, b = 4, c = 6 }
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.shuffle(t)
|
2015-01-10 16:21:57 +00:00
|
|
|
Returns a shuffled copy of the array `t`.
|
2014-02-27 20:19:10 +00:00
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.sort(t [, comp])
|
2015-01-10 16:54:23 +00:00
|
|
|
Returns a copy of the array `t` with all its items sorted. If `comp` is a
|
|
|
|
function it will be used to compare the items when sorting. If `comp` is a
|
2015-01-11 15:27:06 +00:00
|
|
|
string it will be used as the key to sort the items by.
|
2015-01-10 16:54:23 +00:00
|
|
|
```lua
|
|
|
|
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 }
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.array(...)
|
2014-02-27 21:11:31 +00:00
|
|
|
Iterates the supplied iterator and returns an array filled with the values.
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
2017-05-06 07:09:19 +00:00
|
|
|
lume.array(string.gmatch("Hello world", "%a+")) -- Returns {"Hello", "world"}
|
2014-02-27 20:19:10 +00:00
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.each(t, fn, ...)
|
2014-03-03 12:08:11 +00:00
|
|
|
Iterates the table `t` and calls the function `fn` on each value followed by
|
|
|
|
the supplied additional arguments; if `fn` is a string the method of that name
|
|
|
|
is called for each value. The function returns `t` unmodified.
|
|
|
|
```lua
|
|
|
|
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 value
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.map(t, fn)
|
2014-02-27 21:11:31 +00:00
|
|
|
Applies the function `fn` to each value in table `t` and returns a new table
|
|
|
|
with the resulting values.
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
|
|
|
lume.map({1, 2, 3}, function(x) return x * 2 end) -- Returns {2, 4, 6}
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.all(t [, fn])
|
2014-02-28 12:51:50 +00:00
|
|
|
Returns true if all the values in `t` table are true. If a `fn` function is
|
|
|
|
supplied it is called on each value, true is returned if all of the calls to
|
|
|
|
`fn` return true.
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
|
|
|
lume.all({1, 2, 1}, function(x) return x == 1 end) -- Returns false
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.any(t [, fn])
|
2014-02-28 12:51:50 +00:00
|
|
|
Returns true if any of the values in `t` table are true. If a `fn` function is
|
|
|
|
supplied it is called on each value, true is returned if any of the calls to
|
|
|
|
`fn` return true.
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
2014-02-28 12:51:50 +00:00
|
|
|
lume.any({1, 2, 1}, function(x) return x == 1 end) -- Returns true
|
2014-02-27 20:19:10 +00:00
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.reduce(t, fn [, first])
|
2014-02-27 20:19:10 +00:00
|
|
|
Applies `fn` on two arguments cumulative to the items of the array `t`, from
|
2014-03-06 19:33:13 +00:00
|
|
|
left to right, so as to reduce the array to a single value. If a `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
|
2018-03-10 14:57:56 +00:00
|
|
|
an error is raised.
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
2014-03-06 19:33:13 +00:00
|
|
|
lume.reduce({1, 2, 3}, function(a, b) return a + b end) -- Returns 6
|
2014-02-27 20:19:10 +00:00
|
|
|
```
|
|
|
|
|
2018-03-10 14:54:27 +00:00
|
|
|
#### lume.unique(t)
|
2016-02-21 16:43:18 +00:00
|
|
|
Returns a copy of the `t` array with all the duplicate values removed.
|
2014-02-27 22:17:36 +00:00
|
|
|
```lua
|
2018-03-10 14:54:27 +00:00
|
|
|
lume.unique({2, 1, 2, "cat", "cat"}) -- Returns {1, 2, "cat"}
|
2014-02-27 22:17:36 +00:00
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.filter(t, fn [, retainkeys])
|
2014-02-27 22:39:09 +00:00
|
|
|
Calls `fn` on each value of `t` table. Returns a new table with only the values
|
|
|
|
where `fn` returned true. If `retainkeys` is true the table is not treated as
|
|
|
|
an array and retains its original keys.
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
2014-02-27 23:23:07 +00:00
|
|
|
lume.filter({1, 2, 3, 4}, function(x) return x % 2 == 0 end) -- Returns {2, 4}
|
2014-02-27 20:19:10 +00:00
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.reject(t, fn [, retainkeys])
|
2015-02-20 19:22:42 +00:00
|
|
|
The opposite of `lume.filter()`: Calls `fn` on each value of `t` table; returns
|
|
|
|
a new table with only the values where `fn` returned false. If `retainkeys` is
|
|
|
|
true the table is not treated as an array and retains its original keys.
|
|
|
|
```lua
|
2015-02-20 19:51:45 +00:00
|
|
|
lume.reject({1, 2, 3, 4}, function(x) return x % 2 == 0 end) -- Returns {1, 3}
|
2015-02-20 19:22:42 +00:00
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.merge(...)
|
2015-01-10 17:29:12 +00:00
|
|
|
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.
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
2015-01-10 17:29:12 +00:00
|
|
|
lume.merge({a=1, b=2, c=3}, {c=8, d=9}) -- Returns {a=1, b=2, c=8, d=9}
|
2014-02-27 20:19:10 +00:00
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.concat(...)
|
2015-01-10 17:13:15 +00:00
|
|
|
Returns a new array consisting of all the given arrays concatenated into one.
|
|
|
|
```lua
|
|
|
|
lume.concat({1, 2}, {3, 4}, {5, 6}) -- Returns {1, 2, 3, 4, 5, 6}
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.find(t, value)
|
2014-02-27 20:19:10 +00:00
|
|
|
Returns the index/key of `value` in `t`. Returns `nil` if that value does not
|
|
|
|
exist in the table.
|
|
|
|
```lua
|
|
|
|
lume.find({"a", "b", "c"}, "b") -- Returns 2
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.match(t, fn)
|
2014-04-04 17:21:21 +00:00
|
|
|
Returns the value and key of the value in table `t` which returns true when
|
|
|
|
`fn` is called on it. Returns `nil` if no such value exists.
|
|
|
|
```lua
|
|
|
|
lume.match({1, 5, 8, 7}, function(x) return x % 2 == 0 end) -- Returns 8, 3
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.count(t [, fn])
|
2014-04-05 11:21:36 +00:00
|
|
|
Counts the number of values in the table `t`. If a `fn` function is supplied it
|
|
|
|
is called on each value, the number of times it returns true is counted.
|
|
|
|
```lua
|
|
|
|
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 3
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.slice(t [, i [, j]])
|
2014-02-27 20:19:10 +00:00
|
|
|
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.
|
|
|
|
```lua
|
|
|
|
lume.slice({"a", "b", "c", "d", "e"}, 2, 4) -- Returns {"b", "c", "d"}
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.first(t [, n])
|
2014-12-12 21:33:41 +00:00
|
|
|
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"
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.last(t [, n])
|
2014-12-12 21:33:41 +00:00
|
|
|
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"
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.invert(t)
|
2014-03-08 15:18:46 +00:00
|
|
|
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"}
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.pick(t, ...)
|
2015-05-09 14:44:52 +00:00
|
|
|
Returns a copy of the table filtered to only contain values for the given keys.
|
2015-05-09 14:42:38 +00:00
|
|
|
```lua
|
|
|
|
lume.pick({ a = 1, b = 2, c = 3 }, "a", "c") -- Returns { a = 1, c = 3 }
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.keys(t)
|
2014-12-12 20:37:07 +00:00
|
|
|
Returns an array containing each key of the table.
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.clone(t)
|
2014-02-27 20:19:10 +00:00
|
|
|
Returns a shallow copy of the table `t`.
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.fn(fn, ...)
|
2014-02-27 20:19:10 +00:00
|
|
|
Creates a wrapper function around function `fn`, automatically inserting the
|
2014-03-03 13:34:27 +00:00
|
|
|
arguments into `fn` which 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 to `fn`.
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
|
|
|
local f = lume.fn(print, "Hello")
|
2014-03-03 13:34:27 +00:00
|
|
|
f("world") -- Prints "Hello world"
|
2014-02-27 20:19:10 +00:00
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.once(fn, ...)
|
2014-03-02 00:09:24 +00:00
|
|
|
Returns a wrapper function to `fn` which takes the supplied arguments. The
|
|
|
|
wrapper function will call `fn` on the first call and do nothing on any
|
|
|
|
subsequent calls.
|
|
|
|
```lua
|
|
|
|
local f = lume.once(print, "Hello")
|
|
|
|
f() -- Prints "Hello"
|
|
|
|
f() -- Does nothing
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.memoize(fn)
|
2014-05-01 17:58:29 +00:00
|
|
|
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)
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.combine(...)
|
2014-04-03 19:09:56 +00:00
|
|
|
Creates a wrapper function which calls each supplied argument in the order they
|
2014-04-28 11:47:42 +00:00
|
|
|
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.
|
2014-04-03 19:09:56 +00:00
|
|
|
```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
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.call(fn, ...)
|
2015-01-10 16:55:17 +00:00
|
|
|
Calls the given function with the provided arguments and returns its values. If
|
|
|
|
`fn` is `nil` then no action is performed and the function returns `nil`.
|
2015-01-10 16:29:13 +00:00
|
|
|
```lua
|
2015-01-10 16:55:17 +00:00
|
|
|
lume.call(print, "Hello world") -- Prints "Hello world"
|
2015-01-10 16:29:13 +00:00
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.time(fn, ...)
|
2014-05-01 18:23:44 +00:00
|
|
|
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.
|
|
|
|
```lua
|
|
|
|
lume.time(function(x) return x end, "hello") -- Returns 0, "hello"
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.lambda(str)
|
2014-03-08 21:10:24 +00:00
|
|
|
Takes a string lambda and returns a function. `str` should be a list of
|
2014-03-18 18:01:12 +00:00
|
|
|
comma-separated parameters, followed by `->`, followed by the expression which
|
2014-03-08 21:10:24 +00:00
|
|
|
will be evaluated and returned.
|
|
|
|
```lua
|
|
|
|
local f = lume.lambda "x,y -> 2*x+y"
|
|
|
|
f(10, 5) -- Returns 25
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.serialize(x)
|
2014-02-27 21:11:31 +00:00
|
|
|
Serializes the argument `x` into a string which can be loaded again using
|
2014-02-28 13:05:34 +00:00
|
|
|
`lume.deserialize()`. Only booleans, numbers, tables and strings can be
|
2015-09-04 17:59:59 +00:00
|
|
|
serialized. Circular references will result in an error; all nested tables are
|
2014-02-28 13:05:34 +00:00
|
|
|
serialized as unique tables.
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
|
|
|
lume.serialize({a = "test", b = {1, 2, 3}, false})
|
|
|
|
-- Returns "{[1]=false,["a"]="test",["b"]={[1]=1,[2]=2,[3]=3,},}"
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.deserialize(str)
|
2014-02-27 20:19:10 +00:00
|
|
|
Deserializes a string created by `lume.serialize()` and returns the resulting
|
|
|
|
value. This function should not be run on an untrusted string.
|
|
|
|
```lua
|
|
|
|
lume.deserialize("{1, 2, 3}") -- Returns {1, 2, 3}
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.split(str [, sep])
|
2014-03-19 19:26:55 +00:00
|
|
|
Returns an array of the words in the string `str`. If `sep` is provided it is
|
|
|
|
used as the delimiter, consecutive delimiters are not grouped together and will
|
|
|
|
delimit empty strings.
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
2014-02-27 22:51:22 +00:00
|
|
|
lume.split("One two three") -- Returns {"One", "two", "three"}
|
2014-03-19 13:02:31 +00:00
|
|
|
lume.split("a,b,,c", ",") -- Returns {"a", "b", "", "c"}
|
2014-02-27 20:19:10 +00:00
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.trim(str [, chars])
|
2014-02-27 20:19:10 +00:00
|
|
|
Trims the whitespace from the start and end of the string `str` and returns the
|
|
|
|
new string. If a `chars` value is set the characters in `chars` are trimmed
|
2014-02-27 21:11:31 +00:00
|
|
|
instead of whitespace.
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
|
|
|
lume.trim(" Hello ") -- Returns "Hello"
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.wordwrap(str [, limit])
|
2015-01-10 20:26:19 +00:00
|
|
|
Returns `str` wrapped to `limit` number of characters per line, by default
|
|
|
|
`limit` is `72`. `limit` can also be a function which when passed a string,
|
|
|
|
returns `true` if it is too long for a single line.
|
|
|
|
```lua
|
|
|
|
-- Returns "Hello world\nThis is a\nshort string"
|
|
|
|
lume.wordwrap("Hello world. This is a short string", 14)
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.format(str [, vars])
|
2014-02-27 20:19:10 +00:00
|
|
|
Returns a formatted string. The values of keys in the table `vars` can be
|
2014-03-04 12:18:50 +00:00
|
|
|
inserted into the string by using the form `"{key}"` in `str`; numerical keys
|
|
|
|
can also be used.
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
2014-03-04 12:18:50 +00:00
|
|
|
lume.format("{b} hi {a}", {a = "mark", b = "Oh"}) -- Returns "Oh hi mark"
|
|
|
|
lume.format("Hello {1}!", {"world"}) -- Returns "Hello world!"
|
2014-02-27 20:19:10 +00:00
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.trace(...)
|
2014-03-02 20:48:21 +00:00
|
|
|
Prints the current filename and line number followed by each argument separated
|
|
|
|
by a space.
|
|
|
|
```lua
|
|
|
|
-- Assuming the file is called "example.lua" and the next line is 12:
|
2015-08-14 19:56:41 +00:00
|
|
|
lume.trace("hello", 1234) -- Prints "example.lua:12: hello 1234"
|
2014-03-02 20:48:21 +00:00
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.dostring(str)
|
2014-02-27 20:19:10 +00:00
|
|
|
Executes the lua code inside `str`.
|
|
|
|
```lua
|
|
|
|
lume.dostring("print('Hello!')") -- Prints "Hello!"
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.uuid()
|
2014-04-05 13:41:31 +00:00
|
|
|
Generates a random UUID string; version 4 as specified in
|
|
|
|
[RFC 4122](http://www.ietf.org/rfc/rfc4122.txt).
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.hotswap(modname)
|
2014-03-03 12:11:57 +00:00
|
|
|
Reloads an already loaded module in place, allowing you to immediately see the
|
2014-03-02 17:13:18 +00:00
|
|
|
effects of code changes without having to restart the program. `modname` should
|
|
|
|
be the same string used when loading the module with require(). In the case of
|
|
|
|
an error the global environment is restored and `nil` plus an error message is
|
|
|
|
returned.
|
2014-03-01 15:05:51 +00:00
|
|
|
```lua
|
|
|
|
lume.hotswap("lume") -- Reloads the lume module
|
2014-03-02 17:13:18 +00:00
|
|
|
assert(lume.hotswap("inexistant_module")) -- Raises an error
|
2014-03-01 15:05:51 +00:00
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.ripairs(t)
|
2015-01-11 14:58:12 +00:00
|
|
|
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.
|
|
|
|
```lua
|
|
|
|
-- Prints "3->c", "2->b" and "1->a" on separate lines
|
|
|
|
for i, v in lume.ripairs({ "a", "b", "c" }) do
|
|
|
|
print(i .. "->" .. v)
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.color(str [, mul])
|
2015-05-09 14:18:16 +00:00
|
|
|
Takes color string `str` and returns 4 values, one for each color channel (`r`,
|
|
|
|
`g`, `b` and `a`). By default the returned values are between 0 and 1; the
|
|
|
|
values are multiplied by the number `mul` if it is provided.
|
|
|
|
```lua
|
2015-05-09 14:21:08 +00:00
|
|
|
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, 256
|
2015-05-09 14:18:16 +00:00
|
|
|
```
|
|
|
|
|
2017-05-06 07:11:21 +00:00
|
|
|
#### lume.chain(value)
|
2014-04-05 15:30:10 +00:00
|
|
|
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.
|
|
|
|
```lua
|
|
|
|
lume.chain({1, 2, 3, 4})
|
|
|
|
:filter(function(x) return x % 2 == 0 end)
|
|
|
|
:map(function(x) return -x end)
|
|
|
|
:result() -- Returns { -2, -4 }
|
|
|
|
```
|
2014-12-13 01:00:47 +00:00
|
|
|
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
|
|
|
|
```
|
2014-04-05 15:30:10 +00:00
|
|
|
|
2015-02-14 16:02:40 +00:00
|
|
|
## Iteratee functions
|
|
|
|
Several lume functions allow a `table`, `string` or `nil` to be used in place
|
|
|
|
of their iteratee function argument. The functions that provide this behaviour
|
2015-02-20 19:27:34 +00:00
|
|
|
are: `map()`, `all()`, `any()`, `filter()`, `reject()`, `match()` and
|
|
|
|
`count()`.
|
2015-02-14 16:02:40 +00:00
|
|
|
|
|
|
|
If the argument is `nil` then each value will return itself.
|
|
|
|
```lua
|
|
|
|
lume.filter({ true, true, false, true }, nil) -- { true, true, true }
|
|
|
|
```
|
|
|
|
|
|
|
|
If the argument is a `string` then each value will be assumed to be a table,
|
|
|
|
and will return the value of the key which matches the string.
|
|
|
|
``` lua
|
|
|
|
local t = {{ z = "cat" }, { z = "dog" }, { z = "owl" }}
|
|
|
|
lume.map(t, "z") -- Returns { "cat", "dog", "owl" }
|
|
|
|
```
|
|
|
|
|
|
|
|
If the argument is a `table` then each value will return `true` or `false`,
|
|
|
|
depending on whether the values at each of the table's keys match the
|
2015-02-14 16:09:43 +00:00
|
|
|
collection's value's values.
|
2015-02-14 16:02:40 +00:00
|
|
|
```lua
|
|
|
|
local t = {
|
|
|
|
{ age = 10, type = "cat" },
|
|
|
|
{ age = 8, type = "dog" },
|
|
|
|
{ age = 10, type = "owl" },
|
|
|
|
}
|
|
|
|
lume.count(t, { age = 10 }) -- returns 2
|
|
|
|
```
|
|
|
|
|
2014-02-27 20:19:10 +00:00
|
|
|
|
|
|
|
## License
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the MIT license. See [LICENSE](LICENSE) for details.
|