Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Add Array#first= and Array#last= to std lib (http://www.velocityreviews.com/forums/t846937-add-array-first-and-array-last-to-std-lib.html)

Robert Klemme 12-27-2007 09:59 AM

Add Array#first= and Array#last= to std lib
 
Hi,

just today I came across a situation where I needed Array#last=
because I wanted to do

an_array.last += 1

Does anybody else see this as useful? Any issues with this? If not
I'll open an RCR.

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end


Joe 12-27-2007 02:38 PM

Re: Add Array#first= and Array#last= to std lib
 
Is there a problem with doing something like this:

an_array[-1] += 1

Joe

On Dec 27, 2007 4:59 AM, Robert Klemme <shortcutter@googlemail.com> wrote:
> Hi,
>
> just today I came across a situation where I needed Array#last=
> because I wanted to do
>
> an_array.last += 1
>
> Does anybody else see this as useful? Any issues with this? If not
> I'll open an RCR.
>
> Kind regards
>
> robert
>
> --
> use.inject do |as, often| as.you_can - without end
>
>



George 12-27-2007 03:45 PM

Re: Add Array#first= and Array#last= to std lib
 
On Dec 27, 2007 8:59 PM, Robert Klemme <shortcutter@googlemail.com> wrote:
> Hi,
>
> just today I came across a situation where I needed Array#last=
> because I wanted to do
>
> an_array.last += 1
>
> Does anybody else see this as useful? Any issues with this? If not
> I'll open an RCR.


I'll bite... ;-)

What would you expect this to do?

[].last = 1

Regards,
George.


Sebastian Hungerecker 12-27-2007 03:55 PM

Re: Add Array#first= and Array#last= to std lib
 
George wrote:
> What would you expect this to do?
>
> =A0 [].last =3D 1


The same thing as "[][-1] =3D 1", I'd imagine.
The problem I'm seeing would be this: If you allow arr.last =3D x, you'd al=
so=20
have to allow arr.last(n) =3D x if you want to be consistent, but that's no=
t=20
syntactically possible.


=2D-=20
NP: Katatonia - Endtime
Jabber: sepp2k@jabber.org
ICQ: 205544826


MonkeeSage 12-27-2007 05:53 PM

Re: Add Array#first= and Array#last= to std lib
 
On Dec 27, 9:55 am, Sebastian Hungerecker <sep...@googlemail.com>
wrote:
> George wrote:
> > What would you expect this to do?

>
> > [].last = 1

>
> The same thing as "[][-1] = 1", I'd imagine.
> The problem I'm seeing would be this: If you allow arr.last = x, you'd also
> have to allow arr.last(n) = x if you want to be consistent, but that's not
> syntactically possible.
>
> --
> NP: Katatonia - Endtime
> Jabber: sep...@jabber.org
> ICQ: 205544826


Agree. It's tempting to treat #first / #last as 0 / -1, but in
actuality they are method calls and simply return a value; they don't
subscript an array. Setting #last is not semantically different than
[1,2,3].pop = 4, it's just that #last is just a bit more subtle.

Regards,
Jordan

Trans 12-27-2007 06:52 PM

Re: Add Array#first= and Array#last= to std lib
 


On Dec 27, 12:54 pm, MonkeeSage <MonkeeS...@gmail.com> wrote:
> On Dec 27, 9:55 am, Sebastian Hungerecker <sep...@googlemail.com>
> wrote:
>
> > George wrote:
> > > What would you expect this to do?

>
> > > [].last = 1

>
> > The same thing as "[][-1] = 1", I'd imagine.
> > The problem I'm seeing would be this: If you allow arr.last = x, you'd also
> > have to allow arr.last(n) = x if you want to be consistent, but that's not
> > syntactically possible.

>
> > --
> > NP: Katatonia - Endtime
> > Jabber: sep...@jabber.org
> > ICQ: 205544826

>
> Agree. It's tempting to treat #first / #last as 0 / -1, but in
> actuality they are method calls and simply return a value; they don't
> subscript an array. Setting #last is not semantically different than
> [1,2,3].pop = 4, it's just that #last is just a bit more subtle.


I don't see what you are getting at here. #pop is destructive, #last
is not. What does #last return when it is called? It returns a
reference to the last element. So why would #last= do anything other
then set the reference of the last element? Seems obvious to me. So we
can't do last(n) = x, due to syntax constraints, oh well. It would
still be convenient to have the obvious n=1, no arg case. I find that
my programs are usually easier to read when I can use words rather non-
alphabetic symbols.

T.


MonkeeSage 12-27-2007 07:15 PM

Re: Add Array#first= and Array#last= to std lib
 
On Dec 27, 12:52 pm, Trans <transf...@gmail.com> wrote:
> On Dec 27, 12:54 pm, MonkeeSage <MonkeeS...@gmail.com> wrote:
>
>
>
> > On Dec 27, 9:55 am, Sebastian Hungerecker <sep...@googlemail.com>
> > wrote:

>
> > > George wrote:
> > > > What would you expect this to do?

>
> > > > [].last = 1

>
> > > The same thing as "[][-1] = 1", I'd imagine.
> > > The problem I'm seeing would be this: If you allow arr.last = x, you'd also
> > > have to allow arr.last(n) = x if you want to be consistent, but that's not
> > > syntactically possible.

>
> > > --
> > > NP: Katatonia - Endtime
> > > Jabber: sep...@jabber.org
> > > ICQ: 205544826

>
> > Agree. It's tempting to treat #first / #last as 0 / -1, but in
> > actuality they are method calls and simply return a value; they don't
> > subscript an array. Setting #last is not semantically different than
> > [1,2,3].pop = 4, it's just that #last is just a bit more subtle.

>
> I don't see what you are getting at here. #pop is destructive, #last
> is not. What does #last return when it is called? It returns a
> reference to the last element. So why would #last= do anything other
> then set the reference of the last element? Seems obvious to me. So we
> can't do last(n) = x, due to syntax constraints, oh well. It would
> still be convenient to have the obvious n=1, no arg case. I find that
> my programs are usually easier to read when I can use words rather non-
> alphabetic symbols.
>
> T.


IOW, #pop returns a value, and this is just what #last does. One could
argue that #last and #[-1] *should be* synonymous (which may be the
point of the proposal); but as it currently stands, #last means the
same thing as `def l(a); a[-1]; end` so it makes no sense to have a
setter for it. Unless the semantics change, (to me at least) it is non-
sense to have #last=. It's the same as a.pop = 3.

Regards,
Jordan

Sebastian Hungerecker 12-27-2007 07:38 PM

Re: Add Array#first= and Array#last= to std lib
 
MonkeeSage wrote:
> IOW, #pop returns a value, and this is just what #last does.


How is [] different in that regard? That only returns a value, too.


--
Jabber: sepp2k@jabber.org
ICQ: 205544826


MonkeeSage 12-27-2007 07:41 PM

Re: Add Array#first= and Array#last= to std lib
 
On Dec 27, 1:38 pm, Sebastian Hungerecker <sep...@googlemail.com>
wrote:
> MonkeeSage wrote:
> > IOW, #pop returns a value, and this is just what #last does.

>
> How is [] different in that regard? That only returns a value, too.
>
> --
> Jabber: sep...@jabber.org
> ICQ: 205544826


Exactly. It's not... [] = 3 => syntax error...

Regards,
Jordan

Sebastian Hungerecker 12-27-2007 07:58 PM

Re: Add Array#first= and Array#last= to std lib
 
MonkeeSage wrote:
> On Dec 27, 1:38 pm, Sebastian Hungerecker wrote:
> > MonkeeSage wrote:
> > > IOW, #pop returns a value, and this is just what #last does.

> >
> > How is [] different in that regard? That only returns a value, too.

>
> Exactly. It's not... [] = 3 => syntax error...


That [] there is an emtpy array. That's not the [] I was talking about.
I'm sorry if I wasn't clear, let me rephrase:
You seemed to say that while there is a method Array#[]= (which I assume,
you're ok with, since you haven't stated otherwise), there shouldn't be a
method Array#last= since Array#last only returns a value like Array#pop does.
Now my question to you is: How is Array#[] different in that regard than
Array#last? I mean some_array[-1] also only returns a value. But you don't
have a problem with people being able to write some_array[-1] = some_value
do you?


--
Jabber: sepp2k@jabber.org
ICQ: 205544826



All times are GMT. The time now is 11:33 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.