Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.postscript > #3699

Re: in-place reverse?

Newsgroups comp.lang.postscript
Date 2021-11-08 10:08 -0800
References <d9c46408-0992-4b61-a35e-85be12aa3b70n@googlegroups.com> <sl3b3v$50o$1@dont-email.me>
Message-ID <a3965da1-8a8e-4ce0-8a42-907599fe901bn@googlegroups.com> (permalink)
Subject Re: in-place reverse?
From luser droog <luser.droog@gmail.com>

Show all headers | View raw


On Sunday, October 24, 2021 at 5:04:49 AM UTC-5, James wrote:
> On 22/10/2021 07:24, luser droog wrote: 
> > I was reading through some old code and found this function to 
> > reverse an array.
> > And I wondered if it were possible to do it in-place, without creating 
> > a new array.
> /reverse { 
> aload dup length 1 sub 
> 0 exch 1 exch 
> { exch dup 4 2 roll exch put } for 
> } def 
> 
> [ 1 2 3 4 5 ] 
> dup 
> reverse 
> pstack

I adopted this version (since most of mine were ridiculous or broken
or both) but with a few tweaks. Substituting "dup ... exch ... exch" with "index",
and rearranging the innards of the loop to use "copy ... pop (pop)" which I'm 
starting to like as an idiom. It's the same number of operators in the loop
but I find the changing stack picture easier to visualize with this rewrite.

    /reverse { aload 0 1 2 index length 1 sub { 3 2 roll 3 copy put pop pop } for } def

Maybe "3 -1 roll" would be more readable (?). Is it more of a "grabbing the 3rd thing"
or "rearranging the 3 things with the top 2 at the bottom"? I'm not sure.

Back to comp.lang.postscript | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

in-place reverse? luser droog <luser.droog@gmail.com> - 2021-10-21 23:24 -0700
  Re: in-place reverse? luser droog <luser.droog@gmail.com> - 2021-10-22 17:13 -0700
    Re: in-place reverse? luser droog <luser.droog@gmail.com> - 2021-10-22 17:42 -0700
    Re: in-place reverse? luser droog <luser.droog@gmail.com> - 2021-10-23 16:54 -0700
  Re: in-place reverse? luser droog <luser.droog@gmail.com> - 2021-10-22 17:16 -0700
  Re: in-place reverse? James <news@oxdrove.co.uk> - 2021-10-24 11:04 +0100
    Re: in-place reverse? luser droog <luser.droog@gmail.com> - 2021-10-24 17:59 -0700
    Re: in-place reverse? luser droog <luser.droog@gmail.com> - 2021-11-08 10:08 -0800
  Re: in-place reverse? David Newall <davidn@davidnewall.com> - 2022-01-21 21:10 +1100
    Re: in-place reverse? luser droog <luser.droog@gmail.com> - 2022-01-21 21:22 -0800
      Re: in-place reverse? jdaw1 <jdawiseman@gmail.com> - 2022-08-10 13:11 -0700

csiph-web