Re: Shorthand for appending to a table

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: Shorthand for appending to a table

Paul Chiusano
What's wrong with t:append(x) ?

Or, if you implement classes via closures, you could even do something like:

t[End] = x   -->  roughly same as t[#t+1], except that t is an object

where 'End' is just a unique value that you use to dispatch on in the __newindex metamethod of t (I can elaborate if anyone is interested). No need for the Lua authors to add anything to get this!

I'm starting to think of programming with raw tables like programming with straight Object arrays in Java -- you don't normally do it unless you're doing something low-level that needs to be super fast, or if you just don't need any convenience methods. I prefer to stick convenience functionality in the methods of a class, not in special purpose syntax. If you have some section of your code that is really speed-critical, you can always cache the methods locally.

-Paul

Reply | Threaded
Open this post in threaded view
|

Re: Shorthand for appending to a table

Inmate2993
I believe Lua doesn't yet have a += operator. It'd make a nice append feature.

LongInconvienietTableName += data;

I'd personally love it as an operator for numbers and strings too, but that's just my C roots showing.

--
William C. Bubel


Reply | Threaded
Open this post in threaded view
|

Re: Shorthand for appending to a table

gary ng
What would it mean if data is a table ? Would it be a
positional "normal add"(like in matrix operation t[x]
= t[x] + data[x] for x <= len of data) or extend or
just add one item data to the table ? What if I want
one behaviour in certain cases and the other in some
other cases ?

--- Inmate2993 <[hidden email]> wrote:

> I believe Lua doesn't yet have a += operator. It'd
> make a nice append 
> feature.
> 
> LongInconvienietTableName += data;
> 
> I'd personally love it as an operator for numbers
> and strings too, but 
> that's just my C roots showing.
> 
> --
> William C. Bubel
> 
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Reply | Threaded
Open this post in threaded view
|

Re: Shorthand for appending to a table

David Given
gary ng wrote:
> What would it mean if data is a table ? Would it be a
> positional "normal add"(like in matrix operation t[x]
> = t[x] + data[x] for x <= len of data) or extend or
> just add one item data to the table ? What if I want
> one behaviour in certain cases and the other in some
> other cases ?

In which case you'd use tables with different metamethods for each kind of
thing --- e.g., a Matrix class if you want member addition behaviour. What
we're currently discussing (I hope) is the default behaviour for tables;
specifically, tables as arrays. (I'd assume that += on a table that wasn't an
array would have the same undefined behaviour as the # operator.)

-- 
+- David Given --McQ-+ "Gaping from its single obling socket was
|  [hidden email]    | scintillating, many fauceted scarlet emerald..."
| ([hidden email]) | --- Jim Theis, _The Eye of Argon_ (spelling
+- www.cowlark.com --+ original)


Attachment: signature.asc
Description: OpenPGP digital signature

Reply | Threaded
Open this post in threaded view
|

Re: Shorthand for appending to a table

gary ng

--- David Given <[hidden email]> wrote:
> In which case you'd use tables with different
> metamethods for each kind of
> thing --- e.g., a Matrix class if you want member
> addition behaviour. What
> we're currently discussing (I hope) is the default
> behaviour for tables;
> specifically, tables as arrays. (I'd assume that +=
> on a table that wasn't an
> array would have the same undefined behaviour as the
> # operator.)
> 
But the "+="(or "+") operator can be quite ambigious
in the context of table, even for just default
behaviour.

t={1,2,3,4}
d={5,6,7,8}

t += d

should I get {1,2,3,4,5,6,7,8} or {1,2,3,4,d} as I see
adding two table together(i.e. extend) is just as
natural as adding two number together. t+d =
{1,2,3,4,5,6,7,8} is the default behaviour in python
list.

that is also true for {1,2,3,4,d}. As d in this case
is being treated just as another object putting at the
end of a table.

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Reply | Threaded
Open this post in threaded view
|

RE: Shorthand for appending to a table

Gavin Kistner-2
In reply to this post by Paul Chiusano
> But the "+="(or "+") operator can be quite ambigious
> in the context of table, even for just default
> behaviour.
> 
> t={1,2,3,4}
> d={5,6,7,8}
> 
> t += d

It's no more ambiguous than
t = t + d
which is what it would/should be shorthand syntactic sugar for.

We already have an __add metamethod, we wouldn't want (IMO) a different
__addassign metamethod.

I too have wanted +=, but at the same time I am repeatedly wowed by the
speed and small size of Lua's interpretter. As I understand it,
simplicity is why += isn't in there; do we really want to start adding
in lots of features if the result is a slower, more bloated Lua? (Death
by a thousand paper cuts is still death.)


Reply | Threaded
Open this post in threaded view
|

RE: Shorthand for appending to a table

gary ng

--- Gavin Kistner <[hidden email]> wrote:

> > But the "+="(or "+") operator can be quite
> ambigious
> > in the context of table, even for just default
> > behaviour.
> > 
> > t={1,2,3,4}
> > d={5,6,7,8}
> > 
> > t += d
> 
> It's no more ambiguous than
> t = t + d
> which is what it would/should be shorthand syntactic
> sugar for.
That is why I think "+" is not the right operator at
all, at least not for "add this to tail". Having
program in python as well, at least I can get similar
behaviour if it is just an extend.

> 
> We already have an __add metamethod, we wouldn't
> want (IMO) a different
> __addassign metamethod.
> 
> I too have wanted +=, but at the same time I am
> repeatedly wowed by the
> speed and small size of Lua's interpretter. As I
> understand it,
> simplicity is why += isn't in there; do we really
> want to start adding
> in lots of features if the result is a slower, more
> bloated Lua? (Death
> by a thousand paper cuts is still death.)
> 
My thought too. It is just not worth it when there is
already insert which serve the purpose, and the
t[#t+1] which make things a bit faster(may be even
more clear of what it does) should it be needed. 



__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com