v0.3
Array module for lua
The array.lua file should be dropped into an existing project and required by it.
Array = require 'array'
arr = Array()
-- add
arr:add(1)
arr:push(true)
arr:insert('hello', 1)
arr:add({a=1})
-- get
ele = arr:get(1)
ele = arr[1]
-- set
arr:set(1, 'hello')
arr[1] = 'hello'
-- pop
ele2 = arr:pop()
arr1 = Array()
arr2 = Array(1, 2, 3) -- 1 2 3
clone a array
arr = Array(1, 2, 3) -- 1 2 3
clone_arr = arr:clone() -- 1 2 3
return true if an array is empty
arr = Array() -- 1 2 3
arr:is_empty() -- true
return count of items
arr = Array(1, 2, 3) -- 1 2 3
arr:size() -- 3
return item which position is the index
arr = Array(1, 2, 3) -- 1 2 3
arr:get(2) -- 2
arr[2] -- 2
set new value which position is the index
arr = Array(1, 2, 3) -- 1 2 3
arr:set(2, 4) -- 1 4 3
arr[2] = 4 -- 1 4 3
add item to the end of array
arr = Array(1, 2, 3) -- 1 2 3
arr:add(4) -- 1 2 3 4
arr:push(5) -- 1 2 3 4 5
insert item to the position
arr = Array(1, 2, 3) -- 1 2 3
arr:insert(0, 2) -- 1 0 2 3
remove item from array
arr = Array(1, 2, 3) -- 1 2 3
arr:remove(2) -- 1 3
remove item from array which position is the index
arr = Array(1, 2, 3) -- 1 2 3
arr:remove_at(1) -- 2 3
return the last item
arr = Array(1, 2, 3) -- 1 2 3
last_item = arr:pop() -- 3
return the index of the item
arr = Array(1, 2, 3) -- 1 2 3
arr:index_of(2) -- 2
return slice between start and finish
arr = Array(1, 2, 3) -- 1 2 3
arr2 = arr:slice(2, 3) -- 2 3
same as slice(start, finish)
, but change self rather than return new array
arr = Array(1, 2, 3) -- 1 2 3
arr:sliced(2, 3) -- 2 3
return reversed array
arr = Array(1, 2, 3) -- 1 2 3
arr2 = arr:reverse() -- 3 2 1
same as reverse()
, but change self rather than return new array
arr = Array(1, 2, 3) -- 1 2 3
arr:reversed() -- 3 2 1
return the first item in array
arr = Array(1, 2, 3) -- 1 2 3
item = arr:first() -- 1
return the last item in array
arr = Array(1, 2, 3) -- 1 2 3
item = arr:last() -- 3
create a new array of values by mapping each value in array through a transformation function
callback(item, index)
arr = Array(1, 2, 3) -- 1 2 3
arr2 = arr:map(function (item, index)
return item + index
end) -- 2 4 6
same as map(callback)
, but change self rather than return new array
arr = Array(1, 2, 3) -- 1 2 3
arr:mapped(function (item, index)
return item + index
end) -- 2 4 6
create a new array containing all elements that pass truth test
callback(item, index)
arr = Array(1, 2, 3) -- 1 2 3
arr2 = arr:map(function (item, index)
return index % 2 == 0
end) -- 2
same as filter(callback)
, but change self rather than return new array
arr = Array(1, 2, 3) -- 1 2 3
arr:filtered(function (item, index)
return index % 2 == 0
end) -- 2
applies a function against an accumulator and each value of the array to reduce it to a single value
arr = Array(1, 2, 3) -- 1 2 3
ret = arr:reduce(function (a, b)
return a + b
end) -- 6
return a new array joining all values from the two tables passed by parameter
arr = Array(1, 2, 3) -- 1 2 3
arr2 = Array(4, 5, 6) -- 4 5 6
arr3 = arr:concat(arr2) -- 1 2 3 4 5 6
return a new array, removing duplicates values
arr = Array(1, 2, 3, 2, 3) -- 1 2 3 2 3
arr2 = arr:unique() -- 1 2 3
same as unique()
, but change self rather than return new array
arr = Array(1, 2, 3, 2, 3) -- 1 2 3 2 3
arr:uniqued() -- 1 2 3