[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

pop/push, shift/unshift

Simon Schuster

10/23/2007 3:53:00 AM

the inconsistency in naming bothers me. :P I would imagine that it
comes from push/pop being older, and shift/unshift being added later
(probably I would guess that both of these are pre-ruby programming
ideas. I know I've seen push/pop before.)

I would like to see either push/pop changed to be
something/unsomething, or shift/unshift changed to be um, visualish
ideas of what's going on (or whatever push/pop is.) my preference
would be push/pop changed to something/unsomething. or maybe
front_add, front_del, back_add, back_del.

just trying to get a gut-grip on these four array processes, and I'm
pushing and popping and shifting and unshifting left and right (and
back and forth?) and it's taking a surprising amount of time to "grok"
them. does anyone think this is a naming convention that could benefit
with some change, or am I just being strange?

60 Answers

Konrad Meyer

10/23/2007 4:47:00 AM

0

Quoth Simon Schuster:
> the inconsistency in naming bothers me. :P I would imagine that it
> comes from push/pop being older, and shift/unshift being added later
> (probably I would guess that both of these are pre-ruby programming
> ideas. I know I've seen push/pop before.)
>
> I would like to see either push/pop changed to be
> something/unsomething, or shift/unshift changed to be um, visualish
> ideas of what's going on (or whatever push/pop is.) my preference
> would be push/pop changed to something/unsomething. or maybe
> front_add, front_del, back_add, back_del.
>
> just trying to get a gut-grip on these four array processes, and I'm
> pushing and popping and shifting and unshifting left and right (and
> back and forth?) and it's taking a surprising amount of time to "grok"
> them. does anyone think this is a naming convention that could benefit
> with some change, or am I just being strange?

Push and pop have been around as instructions since the days of asm. The
common "visualisation" is to imagine a dinner-plate-stack-holder at a buffet.
You push plates down into the spring-loaded stack to add more. When you want
to remove a plate, you pop it out of the top of the stack. That's probably
not the best description of the idea, but you can always google for push/pop.

HTH,
--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertil...

Robert Klemme

10/23/2007 6:06:00 AM

0

On 23.10.2007 05:53, Simon Schuster wrote:
> the inconsistency in naming bothers me. :P I would imagine that it
> comes from push/pop being older, and shift/unshift being added later
> (probably I would guess that both of these are pre-ruby programming
> ideas. I know I've seen push/pop before.)
>
> I would like to see either push/pop changed to be
> something/unsomething, or shift/unshift changed to be um, visualish
> ideas of what's going on (or whatever push/pop is.) my preference
> would be push/pop changed to something/unsomething. or maybe
> front_add, front_del, back_add, back_del.
>
> just trying to get a gut-grip on these four array processes, and I'm
> pushing and popping and shifting and unshifting left and right (and
> back and forth?) and it's taking a surprising amount of time to "grok"
> them. does anyone think this is a naming convention that could benefit
> with some change, or am I just being strange?

I think you just should get used to the terminology. Whatever naming is
choosen it is artificial - you can always argue about whether the end
(highest index) or the beginning (lowest index) of the Array is the top
or bottom of the stack etc. So not changing is definitively the better
choice - just think about all the confusion created by all those people
who use this regularly. Also keep in mind that changing these would
break a hell lot of code.

Why don't you just create yourself a simple test script that uses all of
those methods and invoke it whenever you need to remind yourself of the
proper wording?

Kind regards

robert

Thufir Hawat

10/23/2007 6:08:00 AM

0

Simon Schuster

10/23/2007 9:50:00 AM

0

*yawn* oh, what? people have been using these terms for a long time?
oh wow, yeah, clearly important.

*ahem* but what about the idea that it's four points of inter-related
methods? I guess that's not as important as tradition...

*ahem*

.... I mean, tradition and mathematics, they're key. just look at
edison and tesla. :D

On 10/22/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> On 23.10.2007 05:53, Simon Schuster wrote:
> > the inconsistency in naming bothers me. :P I would imagine that it
> > comes from push/pop being older, and shift/unshift being added later
> > (probably I would guess that both of these are pre-ruby programming
> > ideas. I know I've seen push/pop before.)
> >
> > I would like to see either push/pop changed to be
> > something/unsomething, or shift/unshift changed to be um, visualish
> > ideas of what's going on (or whatever push/pop is.) my preference
> > would be push/pop changed to something/unsomething. or maybe
> > front_add, front_del, back_add, back_del.
> >
> > just trying to get a gut-grip on these four array processes, and I'm
> > pushing and popping and shifting and unshifting left and right (and
> > back and forth?) and it's taking a surprising amount of time to "grok"
> > them. does anyone think this is a naming convention that could benefit
> > with some change, or am I just being strange?
>
> I think you just should get used to the terminology. Whatever naming is
> choosen it is artificial - you can always argue about whether the end
> (highest index) or the beginning (lowest index) of the Array is the top
> or bottom of the stack etc. So not changing is definitively the better
> choice - just think about all the confusion created by all those people
> who use this regularly. Also keep in mind that changing these would
> break a hell lot of code.
>
> Why don't you just create yourself a simple test script that uses all of
> those methods and invoke it whenever you need to remind yourself of the
> proper wording?
>
> Kind regards
>
> robert
>
>

richard.j.dale@gmail.com

10/23/2007 10:37:00 AM

0

On Oct 23, 10:50 am, "Simon Schuster" <significa...@gmail.com> wrote:
> *yawn* oh, what? people have been using these terms for a long time?
> oh wow, yeah, clearly important.
>
> *ahem* but what about the idea that it's four points of inter-related
> methods? I guess that's not as important as tradition...
One of the strengths of Ruby is that it borrows good things from
various other languages such as method names. Often it will have the
Lisp name of a method as well as the Smalltalk equivalent, for
instance 'map' is from Lisp and 'collect' is from Smalltalk although
they are equivalent.

If you're new to programming and Ruby is one of your first languages
that won't be obvious. Pushing and popping items on and off a stack
are basic ideas in Computer Science and it's a good idea to be
familiar with the terms normally used as soon as possible. 'shift' and
'unshift' as Array operations I associate with Perl, and for people
who already know Perl, that is one less new name to learn.

Maybe 'enque' and 'deque' could be used as names for methods to put
items on the front of a Queue and remove items from the back somewhere
in Ruby - then you wouldn't need push/pop and shift/unshift depending
on which end of the queue you were operating on.

But I can't see any point in deliberately ignoring what has come
before - of course it is important to be familiar with terms people
have been using for a long time.

-- Richard

Randy Kramer

10/23/2007 11:40:00 AM

0

On Tuesday 23 October 2007 12:46 am, Konrad Meyer wrote:
> Push and pop have been around as instructions since the days of asm. The
> common "visualisation" is to imagine a dinner-plate-stack-holder at a
buffet.
> You push plates down into the spring-loaded stack to add more. When you want
> to remove a plate, you pop it out of the top of the stack. That's probably
> not the best description of the idea, but you can always google for
push/pop.

I always liked that visualization. The problem for me (and I'm not the OP) is
that in Ruby you have to lift all the plates off, put the new plate on the
bottom, and then put all the plates back. (Likewise when you want to "pop"
that plate.) ;-) (Because Ruby uses push and pop on the end of the array
instead of the beginning.)

It's not a major problem, but it did surprise me the first time I ran into it,
and I suppose this approach is a little easier to implement.

(And, if push and pop were built on a separate "stack" data structure (instead
of being built on the array structure (with various other paradigms, like
being able to access any item in the array via the index), this point would
not come up.)

Randy Kramer

Robert Klemme

10/23/2007 11:45:00 AM

0

2007/10/23, Randy Kramer <rhkramer@gmail.com>:
> On Tuesday 23 October 2007 12:46 am, Konrad Meyer wrote:
> > Push and pop have been around as instructions since the days of asm. The
> > common "visualisation" is to imagine a dinner-plate-stack-holder at a
> buffet.
> > You push plates down into the spring-loaded stack to add more. When you want
> > to remove a plate, you pop it out of the top of the stack. That's probably
> > not the best description of the idea, but you can always google for
> push/pop.
>
> I always liked that visualization. The problem for me (and I'm not the OP) is
> that in Ruby you have to lift all the plates off, put the new plate on the
> bottom, and then put all the plates back. (Likewise when you want to "pop"
> that plate.) ;-) (Because Ruby uses push and pop on the end of the array
> instead of the beginning.)

I'm not sure what exactly you mean. #push and #pop just work as one
would expect from a LIFO.

> It's not a major problem, but it did surprise me the first time I ran into it,
> and I suppose this approach is a little easier to implement.
>
> (And, if push and pop were built on a separate "stack" data structure (instead
> of being built on the array structure (with various other paradigms, like
> being able to access any item in the array via the index), this point would
> not come up.)

Frankly, I don't understand your point. You can use an Array as LIFO
just the same. There is no additional lifting etc. involved. And for
this it's completely irrelevant whether they use lowest or highest
index for their operation.

Kind regards

robert

Randy Kramer

10/23/2007 12:28:00 PM

0

On Tuesday 23 October 2007 07:44 am, Robert Klemme wrote:
> 2007/10/23, Randy Kramer <rhkramer@gmail.com>:
> > On Tuesday 23 October 2007 12:46 am, Konrad Meyer wrote:
> > > Push and pop have been around as instructions since the days of asm. The
> > > common "visualisation" is to imagine a dinner-plate-stack-holder at a
> > buffet.
> > > You push plates down into the spring-loaded stack to add more. When you
want
> > > to remove a plate, you pop it out of the top of the stack. That's
probably
> > > not the best description of the idea, but you can always google for
> > push/pop.
> >
> > I always liked that visualization. The problem for me (and I'm not the
OP) is
> > that in Ruby you have to lift all the plates off, put the new plate on the
> > bottom, and then put all the plates back. (Likewise when you want to
"pop"
> > that plate.) ;-) (Because Ruby uses push and pop on the end of the array
> > instead of the beginning.)
>
> I'm not sure what exactly you mean. #push and #pop just work as one
> would expect from a LIFO.

Well, it was partly an attempt at humor, and partly the result of some strange
coding I did recently in a program to convert files in what I call askRhk03
format to askRhk04 format.

In doing that, I had the need (or it seemed convenient) to both push and pop
things on to the array, but then also access all the items in the array in
order. (I.e., using something like array.each { }

But, the first time I attempted it, without noticing that a push occurred on
the end of the array instead of the beginning, I got the wrong results. (No
big deal, I just used array.reverse first.)

Randy Kramer

> > It's not a major problem, but it did surprise me the first time I ran into
it,
> > and I suppose this approach is a little easier to implement.
> >
> > (And, if push and pop were built on a separate "stack" data structure
(instead
> > of being built on the array structure (with various other paradigms, like
> > being able to access any item in the array via the index), this point
would
> > not come up.)
>
> Frankly, I don't understand your point. You can use an Array as LIFO
> just the same. There is no additional lifting etc. involved. And for
> this it's completely irrelevant whether they use lowest or highest
> index for their operation.




>
> Kind regards
>
> robert
>
>

David A. Black

10/23/2007 12:38:00 PM

0

Trans

10/23/2007 12:45:00 PM

0



On Oct 22, 11:53 pm, "Simon Schuster" <significa...@gmail.com> wrote:
> the inconsistency in naming bothers me. :P I would imagine that it
> comes from push/pop being older, and shift/unshift being added later
> (probably I would guess that both of these are pre-ruby programming
> ideas. I know I've seen push/pop before.)
>
> I would like to see either push/pop changed to be
> something/unsomething, or shift/unshift changed to be um, visualish
> ideas of what's going on (or whatever push/pop is.) my preference
> would be push/pop changed to something/unsomething. or maybe
> front_add, front_del, back_add, back_del.
>
> just trying to get a gut-grip on these four array processes, and I'm
> pushing and popping and shifting and unshifting left and right (and
> back and forth?) and it's taking a surprising amount of time to "grok"
> them. does anyone think this is a naming convention that could benefit
> with some change, or am I just being strange?

Some things you just have to get used to. And some things are worth
getting used to ;)

I think push and pop are worthy, but I've never come to terms with
shift and unshift. Not only do they seem backwards to me, but the
words themselves have little mnemonic value. In my work I often alias
shift as pull. That fits nicely with push and pop. Unfortunately I've
yet to find a better substitute for unshift. I've tried things like
'pot' (for put on top) and 'place', and more recently 'poke', though
traditionally that is more akin to insert, but nothing has really
stood out as of yet.

T.