In article <[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] (Luke Palmer) wrote:

>But what we'd really like to do is: given the user knows what push/pop 
>do, what would they *guess* to mean shift (I tend to think that this 
>is a very good technique for naming).
>And, well, I'm thinking pull.  So it's a toss-up between shift/unshift
>and put/pull.

I think "push" and "pull" fairly naturally could be taken to refer to the 
front of an array; you might even argue that the natural direction for 
something to "pop" is away from you (i.e. off the back)

The problem I have with push/pull referring to opposite ends is that the 
same person is doing the pushing and the pulling, so both words ought to 
apply to the same end of the array (the front end), which violates the 
comp-sci-y expectations.  (And although normally I'm happy to chuck CS 
jargon out the window, because so much of it is really bad, the push/pop 
thing goes pretty deep.)  Not to mention everyone coming from Perl <6.  
Though if we had "push" without "pop", that wouldn't be as bad.

I guess we could always use prepend/append, pull/pop.  You might not guess 
what they meant, but once you know, I think the meanings are reasonably 
"obvious".  (Dislike typing though I may, I'm hesitant to suggest "prep" 
and "app".) 

Hm, actually counting letters,  "prepend" is no longer than "unshift" (and 
if not a real word, is at least used as one more often than "unshift" is).  
In fact, prepend/append/pull/pop altogether are only one letter more than 
push/pop/shift/unshift.  So those are now officially my preferred 
replacements.


But if we want something that makes it immediately obvious what end of the 
array we're messing with.. something visually obvious... ooh, this sounds 
like a job for Unicode!!  (Just kidding.  Sort of.)  We've already got 
those lovely pipe operators to build on, and they can already do 
assignment; if you can go from = to +=, why not from <== to +<==?

     @a <== $foo, $bar;       # @a=($foo, $bar)
     $foo, $bar ==> @a;       # ditto
     
     @a +<== $foo, $bar;      # push @a, $foo, $bar
     $foo, $bar ==>+ @a;      # unshift @a, $foo, $bar
     
     @a -==> $foo, $bar;      # ($bar, $foo) = (pop @a, pop @a)
     $foo, $bar <==- @a;      # ($foo, $bar) = (shift @a, shift @a)

The + or - tells you whether you're adding on or taking away, and the 
arrow points to (or from) the end of the array you're doing it to.  (I 
know some people will hate four symbols in a row.  That's why we have 
seven-letter alternatives like "prepend".)

I was going to say an advantage over old-fashioned pop/shift is that you 
could remove more than one element at a time, but there isn't any reason 
for their P6 versions not to return as many items as are want()ed, is 
there?

The bad news (assuming anyone actually thinks there's anything good in the 
above suggestion) is that since +<== and friends are assignment operators, 
you can't just do foobar( @a-==>, $x, $y).  Um, unless -==> could be made 
to work as a unary operator.  Which even I don't think I like.  =)  So we 
should keep the wordy versions too.



                                        -David "pull goes the weasel" Green

Reply via email to