lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 26 March 2014 20:26, Luiz Henrique de Figueiredo
<lhf@tecgraf.puc-rio.br> wrote:
>> Why not create such a function as a member of the string library?
>> string.concat( "there ", "I", " would look ",1, "st")
>
> How is that different from just this?
>         "there " .. "I" .. " would look ",1 .. "st"

I think people have grown paranoid of using the .. operator because of
the constant talk about the inefficiency of  temporary strings, blah
blah blah.

Perhaps it's good to remind people from time to time that the example
above does not generate temporary strings!

]cat concat.lua

x = "sweet"
y = "Concat"

print("hello "..x.." world! "..y.." is not evil!")

] luac -l concat.lua

main <concat.lua:0,0> (11 instructions at 0x9576b50)
0+ params, 6 slots, 1 upvalue, 0 locals, 8 constants, 0 functions
    1    [2]    SETTABUP     0 -1 -2    ; _ENV "x" "sweet"
    2    [3]    SETTABUP     0 -3 -4    ; _ENV "y" "Concat"
    3    [5]    GETTABUP     0 0 -5    ; _ENV "print"
    4    [5]    LOADK        1 -6    ; "hello "
    5    [5]    GETTABUP     2 0 -1    ; _ENV "x"
    6    [5]    LOADK        3 -7    ; " world! "
    7    [5]    GETTABUP     4 0 -3    ; _ENV "y"
    8    [5]    LOADK        5 -8    ; " is not evil!"
    9    [5]    CONCAT       1 1 5  ; <----------- look, ma! a single
CONCAT call!
    10    [5]    CALL         0 2 1
    11    [5]    RETURN       0 1

....which is not the same as...

]cat concat.lua

x = "sweet"
y = "Concat"

print(((("hello "..x).." world! ")..y).." is not evil!")

]luac -l concat.lua

main <concat.lua:0,0> (14 instructions at 0x8f2eb50)
0+ params, 3 slots, 1 upvalue, 0 locals, 8 constants, 0 functions
    1    [2]    SETTABUP     0 -1 -2    ; _ENV "x" "sweet"
    2    [3]    SETTABUP     0 -3 -4    ; _ENV "y" "Concat"
    3    [5]    GETTABUP     0 0 -5    ; _ENV "print"
    4    [5]    LOADK        1 -6    ; "hello "
    5    [5]    GETTABUP     2 0 -1    ; _ENV "x"
    6    [5]    CONCAT       1 1 2 <--------------- one little piggy
    7    [5]    LOADK        2 -7    ; " world! "
    8    [5]    CONCAT       1 1 2 <--------------- two little piggies
    9    [5]    GETTABUP     2 0 -3    ; _ENV "y"
    10    [5]    CONCAT       1 1 2<--------------- three little piggies
    11    [5]    LOADK        2 -8    ; " is not evil!"
    12    [5]    CONCAT       1 1 2<--------------- four little piggies
    13    [5]    CALL         0 2 1
    14    [5]    RETURN       0 1

....but of course once we respect concat's right-associativity....

]cat concat.lua

x = "sweet"
y = "Concat"

print("hello "..(x..(" world! "..(y.." is not evil!"))))

...then we get the same code as above

]luac -l concat.lua

main <concat.lua:0,0> (11 instructions at 0x8951b50)
0+ params, 6 slots, 1 upvalue, 0 locals, 8 constants, 0 functions
    1    [2]    SETTABUP     0 -1 -2    ; _ENV "x" "sweet"
    2    [3]    SETTABUP     0 -3 -4    ; _ENV "y" "Concat"
    3    [5]    GETTABUP     0 0 -5    ; _ENV "print"
    4    [5]    LOADK        1 -6    ; "hello "
    5    [5]    GETTABUP     2 0 -1    ; _ENV "x"
    6    [5]    LOADK        3 -7    ; " world! "
    7    [5]    GETTABUP     4 0 -3    ; _ENV "y"
    8    [5]    LOADK        5 -8    ; " is not evil!"
    9    [5]    CONCAT       1 1 5
    10    [5]    CALL         0 2 1
    11    [5]    RETURN       0 1


-- Hisham