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
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
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’
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.
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.
Well, I guess then you can simply use Ruby’s built-in ‘alias’.
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.)
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’
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”.
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.
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”.
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’.
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.
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.
… 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?
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.
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.
...... 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?
When you call rb_define_method(), ruby create a node NODE_CFUNC. The
function pointer is stored in this node.
“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.