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


Groups > comp.lang.postscript > #1907 > unrolled thread

Which is more elegant? Which is more PostScript-like (version 2)

Started byjdaw1 <jdawiseman@gmail.com>
First post2014-05-22 14:12 -0700
Last post2014-05-23 00:49 -0700
Articles 6 — 2 participants

Back to article view | Back to comp.lang.postscript


Contents

  Which is more elegant? Which is more PostScript-like (version 2) jdaw1 <jdawiseman@gmail.com> - 2014-05-22 14:12 -0700
    Re: Which is more elegant? Which is more PostScript-like (version 2) luser- -droog <mijoryx@yahoo.com> - 2014-05-22 15:02 -0700
      Re: Which is more elegant? Which is more PostScript-like (version 2) luser- -droog <mijoryx@yahoo.com> - 2014-05-22 15:15 -0700
      Re: Which is more elegant? Which is more PostScript-like (version 2) luser- -droog <mijoryx@yahoo.com> - 2014-05-25 00:08 -0700
    Re: Which is more elegant? Which is more PostScript-like (version 2) jdaw1 <jdawiseman@gmail.com> - 2014-05-22 16:17 -0700
    Re: Which is more elegant? Which is more PostScript-like (version 2) jdaw1 <jdawiseman@gmail.com> - 2014-05-23 00:49 -0700

#1907 — Which is more elegant? Which is more PostScript-like (version 2)

Fromjdaw1 <jdawiseman@gmail.com>
Date2014-05-22 14:12 -0700
SubjectWhich is more elegant? Which is more PostScript-like (version 2)
Message-ID<b5857ab9-2bda-4f40-a1f3-0481c4da2f5c@googlegroups.com>
Need to test eight things for being numeric, and I have a handy-dandy function IsNumber. Which of the following do you prefer, and why?


//true [ X0 X1 Y0 Y1 dX0 dX1 dY0 dY1 ] {IsNumber not {pop //false exit} if} forall

//true mark X0 X1 Y0 Y1 dX0 dX1 dY0 dY1 8 {IsNumber not {cleartomark pop //false mark exit} if} repeat pop



The former is shorter. But the latter avoids the construction of a temporary array, and so avoids the burdening of the garbage collector with its disposal. 

Thoughts? Better? 

(Version 2 as error in untested code of version 1.)

[toc] | [next] | [standalone]


#1908

Fromluser- -droog <mijoryx@yahoo.com>
Date2014-05-22 15:02 -0700
Message-ID<640fca45-057d-42af-824a-d3bbd227c7ee@googlegroups.com>
In reply to#1907
On Thursday, May 22, 2014 4:12:02 PM UTC-5, jdaw1 wrote:
> Need to test eight things for being numeric, and I have a handy-dandy function IsNumber. Which of the following do you prefer, and why?
> 
> 
> //true [ X0 X1 Y0 Y1 dX0 dX1 dY0 dY1 ] {IsNumber not {pop //false exit} if} forall
> 
> //true mark X0 X1 Y0 Y1 dX0 dX1 dY0 dY1 8 {IsNumber not {cleartomark pop //false mark exit} if} repeat pop
> 
> 
> 
> The former is shorter. But the latter avoids the construction of a temporary array, and so avoids the burdening of the garbage collector with its disposal. 
> 
> Thoughts? Better? 
> 
> (Version 2 as error in untested code of version 1.)

I agree with your analysis. I think "better" would have to 
depend on the larger goals of the project: readability or speed.

A third way, inspired by playing with APL, is to generate
a Boolean vector and check that the sum is equal to the length.

[ [ X0 X1 Y0 Y1 dX0 dX1 dY0 dY1 ] {IsNumber{1}{0}ifelse} forall ]
dup length exch 0 exch {add} forall eq

Of course this makes even more arrays. :(

[toc] | [prev] | [next] | [standalone]


#1909

Fromluser- -droog <mijoryx@yahoo.com>
Date2014-05-22 15:15 -0700
Message-ID<d18c9b8e-9ad5-4b2e-b204-deab5b1680fc@googlegroups.com>
In reply to#1908
On Thursday, May 22, 2014 5:02:49 PM UTC-5, luser- -droog wrote:
> 
> A third way, inspired by playing with APL, is to generate
> a Boolean vector and check that the sum is equal to the length.
> 
> [ [ X0 X1 Y0 Y1 dX0 dX1 dY0 dY1 ] {IsNumber{1}{0}ifelse} forall ]
> dup length exch 0 exch {add} forall eq
> 
> Of course this makes even more arrays. :(

Another way using stop.

[ X0 X1 Y0 Y1 dX0 dX1 dY0 dY1 ]
{ {IsNumber not{stop}if} forall } stopped not

This seems to offer the simplest logic while still 
short-circuiting to avoid unnecessary iterations.

[toc] | [prev] | [next] | [standalone]


#1912

Fromluser- -droog <mijoryx@yahoo.com>
Date2014-05-25 00:08 -0700
Message-ID<7dd6951f-3940-4691-8464-b703f72bb48a@googlegroups.com>
In reply to#1908
On Thursday, May 22, 2014 5:02:49 PM UTC-5, luser- -droog wrote:
> On Thursday, May 22, 2014 4:12:02 PM UTC-5, jdaw1 wrote:
> 
> > Need to test eight things for being numeric, and I have a handy-dandy function IsNumber. Which of the following do you prefer, and why?
> > 
> > 
> > //true [ X0 X1 Y0 Y1 dX0 dX1 dY0 dY1 ] {IsNumber not {pop //false exit} if} forall
> > 
> > //true mark X0 X1 Y0 Y1 dX0 dX1 dY0 dY1 8 {IsNumber not {cleartomark pop //false mark exit} if} repeat pop
> > 
> > 
> > 
> > The former is shorter. But the latter avoids the construction of a temporary array, and so avoids the burdening of the garbage collector with its disposal. 
> > 
> > Thoughts? Better? 
> > 
> > (Version 2 as error in untested code of version 1.)
> 
> I agree with your analysis. I think "better" would have to 
> depend on the larger goals of the project: readability or speed.
                                                ^[, respectively]^

I meant these to apply to the two examples. The array version
is much more readable than `mark ... counttomark`. But, the
other may well be faster (and less burden to the garbage collector).

Sadly, postscript often forces you to make these trade-offs.
Named-variables vs. stack manipulation, for one.

> 
> A third way, inspired by playing with APL, is to generate
> a Boolean vector and check that the sum is equal to the length.
> 
> [ [ X0 X1 Y0 Y1 dX0 dX1 dY0 dY1 ] {IsNumber{1}{0}ifelse} forall ]
> dup length exch 0 exch {add} forall eq
> 
> Of course this makes even more arrays. :(

[toc] | [prev] | [next] | [standalone]


#1910

Fromjdaw1 <jdawiseman@gmail.com>
Date2014-05-22 16:17 -0700
Message-ID<a14c067a-3a4b-434d-b648-f3925431906c@googlegroups.com>
In reply to#1907
I had not considered using stopped -- though it does have one deeper execution depth. But nicely avoids the whole pop business. Hmm. 

Goal? More like goals, plural, being the usual: readability, speed, and memory.

[toc] | [prev] | [next] | [standalone]


#1911

Fromjdaw1 <jdawiseman@gmail.com>
Date2014-05-23 00:49 -0700
Message-ID<1d458f8a-e03b-4145-8bb9-c532fe5cc006@googlegroups.com>
In reply to#1907
The code will be deep inside other {...} executable arrays, so all the marks should be immediately executed: //mark.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.postscript


csiph-web