[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: ipairs in Lua 5.3.0-alpha
- From: Jan Behrens <jbe-lua-l@...>
- Date: Mon, 18 Aug 2014 21:23:25 +0200
On Mon, 18 Aug 2014 19:28:29 +0200
Dirk Laurie <dirk.laurie@gmail.com> wrote:
> 2014-08-18 16:35 GMT+02:00 Jan Behrens <jbe-lua-l@public-software-group.org>:
>
> > This just got me to the idea that if ipairs is made to accept
> > functions (or even iterator triplets), we could also implement
> > a generic concat:
> >
> > ==================================================
> > function string.concat(sep, ...)
> > local t = {}
> > for i, v in ipairs(...) do
> > t[i] = tostring(v)
> > end
> > return table.concat(t, sep)
> > end
> > ==================================================
> ...
> > Can anyone come up with a real con?
>
> Only that this was proposed two years ago [1] but the Lua team did not bite.
>
> [1] http://lua-users.org/lists/lua-l/2012-07/msg00220.htm
>
I assume you are referring to:
http://lua-users.org/lists/lua-l/2012-07/msg00220.html ("l" was missing)
My proposal goes beyond applying "tostring" to every value. That would
just be some extra-convenience. My main proposal is to allow ipairs to
be used as a general interface for (ordinal) iteration. Consider this
alternate form of string.concat:
==================================================
function string.concat(sep, ...)
local t = {}
for i, v in ipairs(...) do
t[i] = v
end
return table.concat(t, sep)
end
==================================================
Here, there is no call to "tostring". So that's not my point. My
point is:
If ipairs(...) accepted a function or an iterator triplet (which it
currently doesn't do in Lua 5.3-alpha), then the following code
would work:
do
local letter = nil
local function alphabet() -- some example iterator
if letter == nil then
letter = "a"
elseif letter == "z" then
return nil
else
letter = string.char(string.byte(letter) + 1)
end
return letter
end
print(string.concat(",", alphabet))
-- prints:
-- a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
end
print(string.concat(",", {"a", "b", "c"}))
-- prints:
-- a,b,c
The same would work for file:lines() and other iterators.
Regards
Jan
- References:
- Speed of # operator (Was: ipairs in Lua 5.3.0-alpha), Dirk Laurie
- Re: ipairs in Lua 5.3.0-alpha, Roberto Ierusalimschy
- Re: ipairs in Lua 5.3.0-alpha, Jan Behrens
- Re: ipairs in Lua 5.3.0-alpha, Doug Currie
- Re: ipairs in Lua 5.3.0-alpha, Coda Highland
- Re: ipairs in Lua 5.3.0-alpha, Jan Behrens
- Re: ipairs in Lua 5.3.0-alpha, Andrew Starks
- Re: ipairs in Lua 5.3.0-alpha, Jan Behrens
- Re: ipairs in Lua 5.3.0-alpha, Jan Behrens
- Re: ipairs in Lua 5.3.0-alpha, Jan Behrens
- Re: ipairs in Lua 5.3.0-alpha, Dirk Laurie
- Re: ipairs in Lua 5.3.0-alpha, Jan Behrens
- Re: ipairs in Lua 5.3.0-alpha, Jan Behrens
- Re: ipairs in Lua 5.3.0-alpha, Dirk Laurie