Created
January 25, 2023 19:19
-
-
Save tilkinsc/5e77e108c3ab8a19c47924692d8d6bc6 to your computer and use it in GitHub Desktop.
Lua but Functional
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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