Made it just for fun:
Usage:
And:
Obs: Don't do something like this:
It will concatenate the strings first and thats not the point of this function.
Code:
Stream = {}
setmetatable(Stream, {__call =
function(_, ...)
local args = {...}
local strtab = {}
for i = 1, #args do
if type(args[i]) == "string" then
table.insert(strtab, args[i])
elseif type(args[i]) == "table" and args[i].strtab then
for s = 1, #args[i].strtab do
table.insert(strtab, args[i].strtab[s])
end
end
end
return setmetatable({strtab = strtab}, {__index = Stream, __concat =
function(self, str)
if type(str) == "string" then
table.insert(self.strtab, str)
elseif type(str) == "table" and str.strtab then
for s = 1, #str.strtab do
table.insert(self.strtab, str.strtab[s])
end
end
return self
end
})
end
})
function Stream:str()
return table.concat(self.strtab)
end
Usage:
Code:
local ss = Stream("a", "b", "c")
print(ss:str()) -- abc
local ss2 = Stream("d", "e", "f")
print(ss2:str()) -- def
ss = ss .. ss2
print(ss:str()) -- abcdef
print(ss2:str()) -- def
And:
Code:
local ss = Stream("mkalo")
print(ss:str()) -- mkalo
local ss2 = Stream(ss, " cezar")
print(ss2:str()) -- mkalo cezar
Obs: Don't do something like this:
Code:
ss = ss .. "a" .. "b" .. "c"
It will concatenate the strings first and thats not the point of this function.
Last edited: