Skip to content

Commit

Permalink
feat: add table functions
Browse files Browse the repository at this point in the history
  • Loading branch information
TorchedSammy committed Oct 17, 2021
1 parent 3158b10 commit 796e8de
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ and all functions will be added.
#### string
`split(str, delim)` > Divides a string into a table of substrings separated by `delim`.

#### table
`filter(t, fn)` > Returns a table with all elements of `t` that pass the filter provided by `fn`.
`map(t, fn)` > Returns a new table with the results of calling `fn` on the elements in `t`.

# License
MIT

20 changes: 20 additions & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,23 @@ string.split = function(str, delimiter)

return res
end

-- table functions
table.filter = function(t, fn)
local res = {}

for k, v in pairs(t) do
if fn(v, k, t) then table.insert(res, v) end
end

return res
end

table.map = function(t, fn)
local res = {}

for k, v in pairs(t) do
table.insert(res, fn(v, k, t))
end
end

0 comments on commit 796e8de

Please sign in to comment.