[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
**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 17:43:54 +0200

2018-07-23 16:07 GMT+02:00 Scott Morgan <blumf@blueyonder.co.uk>:
> On 23/07/18 14:26, Axel Kittenberger wrote:
>>> To write an efficient pure Lua routine is possible, but quite challenging.
>>
>> an O( n * log( n ) ) one:
>> """
>> function _concat( t, p, n )
>> if n > 1 then
>> local n2 = n // 2
>> return _concat( t, p, n2 ) .. _concat( t, p + n2, n - n2 )
>> end
>> return t[ p ]
>> end
>>
>> function concat( tbl )
>> return _concat( tbl, 1, #tbl )
>> end
>> """
>> For lua < 5.3 use math.floor( n / 2) or math.ceil, or round or whatever, it
>> doesn't matter.
>
> How about:
>
> function concat(list, sep, i, j)
> i = i or 1
> j = j or #list
>
> local tmp = {}
> for i=1,#list do
> tmp[i] = tostring(list[i])
> end
> return table.concat(tmp, sep, i, j)
> end
You are not entitled to assume that obj1..obj2 is defined as
tostring(obj1)..tostring(obj2). You are not even entitled to
assume that the result of concatenation is string-valued.
For example, concat may mean list concatenation.
obj{1,2,3}..obj{4,5} --> obj{1,2,3,4,5}