[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Symmetry between language and C API (Was: (not) handling new programming idioms with grace
- From: Dirk Laurie <dirk.laurie@...>
- Date: Mon, 23 Jul 2018 14:59:04 +0200
2018-07-23 11:55 GMT+02:00 Luiz Henrique de Figueiredo <email@example.com>:
>> One place where the symmetry currently is broken is concatenation.
>> The C API function lua_concat respects metamethods, the table.concat
>> function does not.
> The fair comparison is lua_concat vs the concatenation operator: both
> respect metamethods.
The problem with the concatenation operator is that it concatenates
just two values. If I concatenate everything in a table, respecting
metamethods, by the algorithm 
local s = ''
for k,v in ipairs(tbl) do s = s..v end
it takes O(n²) time, where n=#tbl, not to mention the memory problems.
To write an efficient pure Lua routine is possible, but quite challenging.
 The present implementation of 'lub.join' in the LuaRock 'lub' does
just that. I became aware of the problem when using the LuaRock
'xml' which depends on 'lub.join'. For that application, the workaround
lub.join = table.concat is possible, but there may be lub applications
that rely on metamethods being used.