RCR: Stack, Queue alias methods in Array

Rationale: Ruby arrays can be easily used as stacks and queues, but it’s
distracting to have to stop and think about which end of the array
you’re working with.

Proposed aliases:

top <- last
enqueue, append <- push
dequeue <- shift
head <- [0]
tail <- last

martin

How about defining these in modules, so that they can be included into
individual arrays or into the entire array class if needed.

E.g.

require ‘arrayqueue’
require ‘arraystack’

my_queue = [1,2,3]
my_queue.extend ArrayQueue

my_stack = [-1,-2,-3]
my_stack.extend ArrayStack

my_stack.push( my_queue.dequeue )
my_queue.enqueue( my_stack.pop )

Where arraystack.rb would contain:

module ArrayStack
def push( value )

end
def pop

end
def top

end
end

Etc.

···

On Tue, 2002-11-26 at 09:46, Martin DeMello wrote:

Rationale: Ruby arrays can be easily used as stacks and queues, but it’s
distracting to have to stop and think about which end of the array
you’re working with.

Proposed aliases:

top ← last
enqueue, append ← push
dequeue ← shift
head ← [0]
tail ← last


Nat Pryce nat.pryce@b13media.com
B13media

Hi,

Proposed aliases:

top ← last
enqueue, append ← push
dequeue ← shift
head ← [0]
tail ← last

top “top” returns last item? probably confusing.
append we already have append.
enqueue possible, interesting, little bit hard to remember the spell.
dequeue ditto.
head maybe, but…
tail some may feel “tail” must return everything but first.

						matz.
···

In message “RCR: Stack, Queue alias methods in Array” on 02/11/26, Martin DeMello martindemello@yahoo.com writes:

They’re not really new functionality, though, just aliases for existing
methods. And ‘push’ and ‘pop’ are already part of Array - it seems a bit
heavy conceptually to include a module just to ‘alias top last’

···

Nat Pryce nat.pryce@b13media.com wrote:

On Tue, 2002-11-26 at 09:46, Martin DeMello wrote:

Rationale: Ruby arrays can be easily used as stacks and queues, but it’s
distracting to have to stop and think about which end of the array
you’re working with.

Proposed aliases:

top ← last
enqueue, append ← push
dequeue ← shift
head ← [0]
tail ← last

How about defining these in modules, so that they can be included into
individual arrays or into the entire array class if needed.

But that confusion is exactly the reason why it is needed: unless a builtin
Stack class is introduced: without it there is always a 1-minute head
scratching break to remember which end push operates on.

class Array
alias top last
end

a =
a.push 1
a.push 2
a.top # => 2

– Nikodemus

···

On Tue, 26 Nov 2002, Yukihiro Matsumoto wrote:

top “top” returns last item? probably confusing.

Hi,

Proposed aliases:

top ← last
enqueue, append ← push
dequeue ← shift
head ← [0]
tail ← last

top “top” returns last item? probably confusing.
append we already have append.

Oops, so we do. ‘dequeue’ is the natural opposite, then.

enqueue possible, interesting, little bit hard to remember the spell.
dequeue ditto.

They’re the usual textbook terms for queue operations, though, so it’ll
be familiar to at least some fraction of people. Texts tend to vary
between ‘enqueue’ and ‘append’, but they all use ‘dequeue’ for the
“inverse” operation (not really an inverse, since a.enqueue(a.dequeue)
!= a)

head maybe, but…
tail some may feel “tail” must return everything but first.

There’s always cdr for that :slight_smile:

martin

···

Yukihiro Matsumoto matz@ruby-lang.org wrote:

In message “RCR: Stack, Queue alias methods in Array” > on 02/11/26, Martin DeMello martindemello@yahoo.com writes:

Well, I guess then you can simply use Ruby’s built-in ‘alias’. :slight_smile:

If you want something more efficient than this, then you need to modify
the Ruby C source code. (It is not that hard, just add the method name
and the same function pointer.)

Regards,

Bill

···

Martin DeMello martindemello@yahoo.com wrote:

They’re not really new functionality, though, just aliases for existing
methods. And ‘push’ and ‘pop’ are already part of Array - it seems a bit
heavy conceptually to include a module just to ‘alias top last’

Hi,

···

In message “Re: RCR: Stack, Queue alias methods in Array” on 02/11/26, Nikodemus Siivola tsiivola@cc.hut.fi writes:

On Tue, 26 Nov 2002, Yukihiro Matsumoto wrote:

top “top” returns last item? probably confusing.

But that confusion is exactly the reason why it is needed: unless a builtin
Stack class is introduced: without it there is always a 1-minute head
scratching break to remember which end push operates on.

But still, array is array. If you see “top” being last without
knowing arrays can behave as stacks, the confusion is far stronger
than lacking “top”.

						matz.

Well, I guess then you can simply use Ruby's built-in 'alias'. :slight_smile:
If you want something more efficient than this,

alias is efficient

Guy Decoux

Well, it may not be intuitively obvious if you’re not thinking in terms
of stacks, but all that that would need would be for the documentation
to say “the top of the array when viewed as a stack, i.e. the element
that ‘pop’ would return”. It’s not confusing in the sense that one would
keep forgetting what it meant and having to look it up (and not nearly
as confusing as rassoc, for instance). Again, if you weren’t thinking in
terms of stacks, the usual way to visualise an array is horizontally, so
you’d never use ‘top’. It just plugs the hole in the stack abstraction
neatly.

martin

···

Yukihiro Matsumoto matz@ruby-lang.org wrote:

In message “Re: RCR: Stack, Queue alias methods in Array” > on 02/11/26, Nikodemus Siivola tsiivola@cc.hut.fi writes:

On Tue, 26 Nov 2002, Yukihiro Matsumoto wrote:

top “top” returns last item? probably confusing.

But that confusion is exactly the reason why it is needed: unless a builtin
Stack class is introduced: without it there is always a 1-minute head
scratching break to remember which end push operates on.

But still, array is array. If you see “top” being last without
knowing arrays can behave as stacks, the confusion is far stronger
than lacking “top”.

I do :slight_smile: Just thought it’d be a nice thing to have as standard. And
‘head’ at least isn’t defined other than Array#[0].

martin

···

William Djaja Tjokroaminata billtj@z.glue.umd.edu wrote:

Martin DeMello martindemello@yahoo.com wrote:

They’re not really new functionality, though, just aliases for existing
methods. And ‘push’ and ‘pop’ are already part of Array - it seems a bit
heavy conceptually to include a module just to ‘alias top last’

Well, I guess then you can simply use Ruby’s built-in ‘alias’. :slight_smile:

Ooops, sorry. Yes, in fact in “eval.c” the function “rb_alias” has
inserted the same function pointer for the new method name. So an aliased
method call is as efficient as the original method call.

Regards,

Bill

···

ts decoux@moulon.inra.fr wrote:

Well, I guess then you can simply use Ruby’s built-in ‘alias’. :slight_smile:
If you want something more efficient than this,

alias is efficient

Hi,

···

In message “Re: RCR: Stack, Queue alias methods in Array” on 02/11/27, Martin DeMello martindemello@yahoo.com writes:

Well, it may not be intuitively obvious if you’re not thinking in terms
of stacks, but all that that would need would be for the documentation
to say “the top of the array when viewed as a stack, i.e. the element
that ‘pop’ would return”. It’s not confusing in the sense that one would
keep forgetting what it meant and having to look it up (and not nearly
as confusing as rassoc, for instance). Again, if you weren’t thinking in
terms of stacks, the usual way to visualise an array is horizontally, so
you’d never use ‘top’. It just plugs the hole in the stack abstraction
neatly.

“top” is too generic. One can think of arbitrary edge of an array,
and I’m afraid too many guess wrong side. I did, for example.

						matz.

And ‘head’ at least isn’t defined other than Array#[0].

What about Array#first?

Gavin

···

From: “Martin DeMello” martindemello@yahoo.com

… which leads me to ask, why the last time when I asked, given a
method string/name, whether there is an easy way to get the function
pointer, the answer was in principle, no ? (See threads starting at
http://www.ruby-talk.org/46426.)

Or, is it because at the “alias” level, Ruby deals only with nodes and not
the function pointers? But does this then imply that Ruby simply converts
all the functions defined in C to nodes? Does this also imply that in
theory it is possible for me to store the nodes in C to make the C code
run faster?

Regards,

Bill

···

William Djaja Tjokroaminata billtj@z.glue.umd.edu wrote:

Ooops, sorry. Yes, in fact in “eval.c” the function “rb_alias” has
inserted the same function pointer for the new method name. So an aliased
method call is as efficient as the original method call.

(!)

Can’t believe I managed to overlook that in two separate passes through
the method list.

martin

···

Gavin Sinclair gsinclair@soyabean.com.au wrote:

From: “Martin DeMello” martindemello@yahoo.com

And ‘head’ at least isn’t defined other than Array#[0].

What about Array#first?

Yes, but only the first time. Though you’re right, it is less intuitive
than the other methods, since in the case of push and shift there is a
definite edge associated. I guess because programmers have internalised
the fact that arrays grow from the end, making the two edges different in
terms of operation cost.

martin

···

Yukihiro Matsumoto matz@ruby-lang.org wrote:

“top” is too generic. One can think of arbitrary edge of an array,
and I’m afraid too many guess wrong side. I did, for example.

...... which leads me to ask, why the last time when I asked, given a
method string/name, whether there is an easy way to get the function
pointer, the answer was in principle, *no* ? (See threads starting at
http://www.ruby-talk.org/46426\.\)

  http://www.ruby-talk.org/47220

Or, is it because at the "alias" level, Ruby deals only with nodes and not
the function pointers? But does this then imply that Ruby simply converts
all the functions defined in C to nodes?

When you call rb_define_method(), ruby create a node NODE_CFUNC. The
function pointer is stored in this node.

Guy Decoux

Hi,

···

In message “Re: RCR: Stack, Queue alias methods in Array” on 02/11/27, Martin DeMello martindemello@yahoo.com writes:

Yes, but only the first time.

Not for me. I probably have to answer to the question “why top is
last?” thousand times in the future. :wink:

The matz’s first principle of method names.

If you have a “right” name for the method, implement it.
If you have any doubt in a name, just wait.

						matz.

Martin DeMello martindemello@yahoo.com writes:

“top” is too generic. One can think of arbitrary edge of an array,
and I’m afraid too many guess wrong side. I did, for example.

Yes, but only the first time. Though you’re right, it is less
intuitive than the other methods, since in the case of push and
shift there is a definite edge associated. I guess because
programmers have internalised the fact that arrays grow from the
end, making the two edges different in terms of operation cost.

Actually “Array.push” and “Array.shift” are just as confusing to me as
“Array.top” would be. It is all a matter of learning the API.
Compare:

Does Array.push put the element onto the end or the beginning?
Does Array.shift shift the elements to the right or left?
Does Array.top mean the beginning or the end of the array?

They are all similar questions with no obvious answer until you learn
the answer.

···

Yukihiro Matsumoto matz@ruby-lang.org wrote: