Skip to content

Instantly share code, notes, and snippets.

@tilkinsc
Created January 25, 2023 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tilkinsc/5e77e108c3ab8a19c47924692d8d6bc6 to your computer and use it in GitHub Desktop.
Save tilkinsc/5e77e108c3ab8a19c47924692d8d6bc6 to your computer and use it in GitHub Desktop.
Lua but Functional
local sub = string.sub
local concat = table.concat
local insert = table.insert
string.where = function(self, predicate)
local buf = {}
for i = 1, #self do
local ch = sub(self, i, i)
if (predicate(ch)) then
insert(buf, ch)
end
end
return concat(buf)
end
string.every = function(self, predicate)
local buf = {}
for i = 1, #self do
insert(buf, predicate(sub(self, i, i)))
end
return concat(buf)
end
local array_metatable = {
__index = table;
}
array = function(tab)
return setmetatable(tab, array_metatable)
end
table.where = function(self, predicate)
local buf = {}
for i = 1, #self do
local obj = self[i]
if (predicate(obj, i)) then
insert(buf, obj)
end
end
return array(buf)
end
table.print = function(self)
for i, v in next, self do
print(i, v)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment