• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

String streams

Mkalo

ボーカロイド
Senator
Joined
Jun 1, 2011
Messages
1,118
Solutions
55
Reaction score
946
Location
Japan
Made it just for fun:

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:
You know arg is already defined in every table when ... is used as a parameter?
So no need to do this
Code:
local args = {...}
You don't even need that line above, you can just call arg

See :)
Code:
function x(...)
    print(unpack(arg))
end

x(1,2,4,5,6,9)
-- output
1    2    4    5    6    9
 
You know arg is already defined in every table when ... is used as a parameter?
So no need to do this
Code:
local args = {...}
You don't even need that line above, you can just call arg

See :)
Code:
function x(...)
    print(unpack(arg))
end

x(1,2,4,5,6,9)
-- output
1    2    4    5    6    9
Not in Lua 5.3:

Run this code here: http://www.lua.org/cgi-bin/demo
Code:
function bla(...)
    local args = {...}
    print(table.unpack(args)) -- 1 2 3
    print(table.unpack(arg)) -- attempt to get length of a nil value
end

bla(1, 2, 3)

In Lua 5.3 arg is like c++ argv, so if you have:

Code:
print(table.unpack(arg))

And run this code using the interpreter like this:
Code:
lua53.exe test.lua 1 2 3

It will print 1 2 3.
 
Last edited:
I am on my phone atm, i tested it.
It threw an error that unpack was a global nil, i then tested arg's index and it tossed a completely different error wbere didn't exist, but isn't 5.3 still in the development stage not mainstream?
 
I am on my phone atm, i tested it.
It threw an error that unpack was a global nil, i then tested arg's index and it tossed a completely different error wbere didn't exist, but isn't 5.3 still in the development stage not mainstream?
From lua's website:
"Lua 5.3 was released on 12 Jan 2015. Its main new features are integers, bitwise operators, a basic utf-8 library, and support for both 64-bit and 32-bit platforms.

The current release is Lua 5.3.2, released on 30 Nov 2015. "


unpack was renamed to table.unpack in Lua 5.3

And in the change notes you can see:

Lua standalone interpreter
  • can be used as calculator; no need to prefix with '='
  • arg table available to all code
This means you can access the arg table (inputs from standalone interpreter) in any place of the code because before you couldn't in this example:
Code:
function bla(...)
    print(arg[0])
end

print(arg[0])
bla()
(The variable local arg is being forced to change because i put ... in the parameters.)

This will produce the output: (filename is test.lua)
Code:
test.lua
nil

But if you run in Lua 5.3 it will output:
Code:
test.lua
test.lua

Before Lua 5.3 you could only access the arg (from input) inside functions that had ... as parameter (as long as they are not recursive.) with this:
Code:
_G.arg
 
Last edited:
Back
Top