Skip to content

rfl890/lua-array

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

lua-array

Better arrays for Lua.

Array is just a table, so it works as a drop in replacement of a regular array.

Usage:

local Array = require("array")
local MyArray = Array:new()
-- push some values (or initialize with :new({1,2,3})
MyArray:push(1)
MyArray:push(2)
MyArray:push(3)
-- map
local squares = MyArray:map(function(v)
    return v * v
end)
-- join
print(MyArray:join("\n"))

Iteration:

local Array = require("array")
local MyArray = Array:new({"A","B","C"})

MyArray:forEach(function(v) 
    print(v)
end)

Get a random element:

math.randomseed(os.time())
local Array = require("array")
local MyArray = Array:new({1,2,3,4,5,6,7,8,9,10})

print(MyArray:getRandomElement())

Filter:

local Array = require("array")
local MyArray = Array:new({1,2,3,4,5,6,7,8,9,10})

local filtered = MyArray:filter(function(v) 
    return v > 5
end)

Pop:

local Array = require("array")
local MyArray = Array:new({"first", "last"})

local removedItem = MyArray:pop() -- "last"
print(MyArray[1]) -- "first"

Shift:

local Array = require("array")
local MyArray = Array:new({"first", "last"})

local removedItem = MyArray:shift() -- "shift"
print(MyArray[1]) -- "last"

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages