util.array

util.array extends normal Lua tables with sevral convenient methods. In many cases, simply using a normal Lua table and the table library works just as well.

Creating

require "util.array" returns a constructor, simply pass a table to this function to create an array.

Arrays can also be created by collecting the return values of an iterator:

Functions

These methods can be called two ways:

Create new array for result

Transform existing array into result

map

Applies a function to all items.

filter

Filter all items trough a function which decides if the item is added to the result array or not.

sort

Sort the array. Takes an optional comparison function.

pluck

Given array of tables or arrays, return an array consisting of a specific item from those arrays.

reverse

Flip the array on its head.

Methods

Some of these mutates the array.

random

Returns a random item from the array. No mutation.

shuffle

Shuffles the array, ie. sorts it in a random order.

append

Appends an all items from one array to another.

push

Push an item onto the end of the array.

pop

Remove an item from the array.

concat

Turns all items into strings and concatenates them togeather separated by sep.

length

Returns the number of items in the array

Operators

add

Summing two arrays results in a new array that contains all items from both arrays.