Arrays¶
A collection of functions that operate specifically on arrays, defined as tables with just keys 1..n.
-- Examples of valid arrays: {} {"red", "green", "blue"} {"winter", {is = "coming"}, [3] = "again"} {1966, nil, nil} -- Examples of invalid arrays: {1994, nil, 2002} {you = {"know", "nothing"}} {[5] = "gold rings"} 42
These functions can iterate over any Ordered values.
Functions¶
append¶
function dash.append(target, ...) --> T[]
Type
<T>(mut T[], ...Ordered<T> -> T[])
Generics
T -
any
- the primary type (extends any value)
Parameters
target -
mut T[]
- an array (of the primary type) (which can be mutated)... -
...Ordered<T>
- Ordereds (of the primary type) - any number of other arrays
Returns
T[]
- an array (of the primary type)
Examples
dash.append({}, {1, 2, 3}, {4, 5, 6}) --> {1, 2, 3, 4, 5, 6}
dash.append({1, 2, 3}) --> {1, 2, 3}
local list = {"cheese"} dash.append(list, {"nachos"}, {}, {"chillies"}) list --> {"cheese", "nachos", "chillies"}
defaultComparator¶
function dash.defaultComparator(a, b) --> bool
true
if a is typically considered lower
than b.
The default comparator is used by dash.sort and can sort elements of different types, in the order: boolean, number, string, function, CFunction, userdata, and table.
Elements which cannot be sorted naturally will be sorted by their string value.
Type
<T>((T, T) -> bool)
Generics
T -
any
- the primary type (extends any value)
Parameters
a -
T
- the primary typeb -
T
- the primary type
Returns
bool
- a boolean
See
first¶
function dash.first(source, predicate) --> V?
true
for.
If the predicate is not specified, dash.first simply returns the first element of the array.
Type
<T: Iterable<K,V>>(T, (element: V, key: K -> bool) -> V?)
Generics
T -
Iterable<K, V>
- the primary type (extends an Iterable (of the primary key type and the primary value type))
Parameters
source -
T
- the primary typepredicate -
(element: V, key: K) -> bool
- a function (taking element (the primary value type) and key (the primary key type), and returning a boolean) - (default =dash.returns(true)
)
Returns
V?
- the primary value type (optional)
Examples
local names = { "Boromir", "Frodo", "Bilbo" } dash.first(names) --> "Boromir", 1 -- Find a particular value: local firstNameWithF = dash.first(names, function(name) return dash.startsWith(name, "F") end) firstNameWithF --> "Frodo", 2 -- What about a value which doesn't exist? local firstNameWithC = dash.first(names, function(name) return dash.startsWith(name, "C") end) firstNameWithC --> nil -- Find the index of a value: local _, index = dash.first(names, dash.fn:matches("Bilbo")) index --> 2
Usage
- If you need to find a value in a table which isn't an array, use dash.find.
See
last¶
function dash.last(source, predicate) --> V?
true
for.
If the predicate is not specified, dash.last simply returns the last element of the array.
Type
<T: Iterable<K,V>>(T, (element: V, key: K -> bool) -> V?)
Generics
T -
Iterable<K, V>
- the primary type (extends an Iterable (of the primary key type and the primary value type))
Parameters
source -
T
- the primary typepredicate -
(element: V, key: K) -> bool
- a function (taking element (the primary value type) and key (the primary key type), and returning a boolean) - (default =dash.returns(true)
)
Returns
V?
- the primary value type (optional)
Examples
local names = { "Boromir", "Frodo", "Bilbo" } dash.last(names) --> "Bilbo", 3 local lastNameWithB = dash.last(names, dash.fn:startsWith("B")) lastNameWithB --> "Bilbo", 3 local _, key = dash.last(names, dash.fn:matches("Frodo")) key --> 2
See
reduce¶
function dash.reduce(source, handler, initial) --> R
Type
<T, R>(Ordered<T>, (result: R, value: T, key: int -> R), R) -> R
Generics
T -
any
- the primary type (extends any value)R -
any
- the result type (extends any value)
Parameters
source -
Ordered<T>
- an Ordered (of the primary type)handler -
(result: R, value: T, key: int) -> R
- a function (taking result (the result type), value (the primary type) and key (an integer), and returning the result type)initial -
R
- the result type
Returns
R
- the result type
Examples
local sum = dash.reduce({1, 2, 3}, function(result, value) return result + value end, 0) sum --> 6
local recipe = {first = "cheese", second = "nachos", third = "chillies"} local unzipRecipe = dash.reduce(recipe, function(result, value, key) table.insert(result[1], key) table.insert(result[2], value) return result end, {{}, {}}) -- (in some order) unzipRecipe --> {{"first", "third", "second"}, {"cheese", "chillies", "nachos"}}
reverse¶
function dash.reverse(source) --> T[]
Type
<T>(T[] -> T[])
Generics
T -
any
- the primary type (extends any value)
Parameters
source -
T[]
- an array (of the primary type)
Returns
T[]
- an array (of the primary type)
Examples
dash.reverse({1, 2, 4, 3, 5}) --> {5, 3, 4, 2, 1}
shuffle¶
function dash.shuffle(source) --> T[]
Type
<T>(T[] -> T[])
Generics
T -
any
- the primary type (extends any value)
Parameters
source -
T[]
- an array (of the primary type)
Returns
T[]
- an array (of the primary type)
Examples
local teamColors = {"red", "red", "red", "blue", "blue", "blue"} -- (in some order) dash.shuffle(teamColors) --> {"blue", "blue", "red", "blue", "red", "red"}
slice¶
function dash.slice(source, first, last, step) --> T[]
Type
<T>(T[], int?, int?, int? -> T[])
Generics
T -
any
- the primary type (extends any value)
Parameters
source -
T[]
- an array (of the primary type)first -
int?
- an integer (optional) - (default = 1) The index of the first element to include.last -
int?
- an integer (optional) - (default =#source
) The index of the last element to include.step -
int?
- an integer (optional) - (default = 1) What amount to step the index by during iteration.
Returns
T[]
- an array (of the primary type)
Examples
dash.slice({10, 20, 30, 40}) --> {10, 20, 30, 40}
dash.slice({10, 20, 30, 40}, 2) --> {20, 30, 40}
dash.slice({10, 20, 30, 40}, 2, 3) --> {20, 30}
dash.slice({10, 20, 30, 40}, 2, 4, 2) --> {20, 40}
sort¶
function dash.sort(input, comparator) --> T[]
Unlike dash.sort, the comparator to dash.sort is optional, but if defined it can also return a numeric weight or nil as well as a boolean to provide an ordering of the elements.
Type
<T>(T[], (T -> bool | number | nil)? -> T[])
Generics
T -
any
- the primary type (extends any value)
Parameters
input -
T[]
- an array (of the primary type)comparator -
(T) -> bool | number | nil?
- a function (taking the primary type, and returning a boolean or a number or nil) (optional) - should returntrue
orn < 0
if the first element should be before the second in the resulting array, or0
ornil
if the elements have the same order.
Returns
T[]
- an array (of the primary type)
Examples
dash.sort({2, 5, 3}) --> {2, 3, 5}
dash.sort({"use", "the", "force", "Luke"}) --> {"Luke", "force", "the", "use"}
dash.sort({ name = "Luke", health = 50 }, { name = "Yoda", health = 9001 }, { name = "Jar Jar Binks", health = 0 }, function(a, b) return a.health < b.health end) --> the characters sorted in ascending order by their health
sum¶
function dash.sum(source) --> number
Type
Ordered<number> -> number
Parameters
source -
Ordered<number>
- an Ordered (of a number)
Returns
number
- a number
Examples
dash.sum({1, 2, 3}) --> 6