Wednesday, July 24, 2013

PostScript (higher order)

Code is a first class citizen in PostScript. The builtin repeat and forall operators are obvious consumers of code (procedures) but it is also easy to define your own higher order skeletons. For instance the code example at the bottom defines hrepeat as used by Wumpus.

For efficiency reasons I'd like a successor to Wumpus to allow users to create their own drawing procedures, and iterating these drawing procs should be possible in PostScript (rather than having to unroll the iteration skeletons during "compilation" leading to code bloat).



% Stroked circle
% follows the idiom of builtin arc where X Y are parameters
% (i.e. not the ~moveto~ then ~show~ idiom of text)
%
/SCIRC     % X Y R SCIRC
{
  /R exch def
  /Y  exch def
  /X  exch def
  newpath
  X Y R 0.0 360.0 arc
  closepath
  stroke
} bind def





% Font load
/FL   % SZ NAME FL
{
  findfont exch
  scalefont
  setfont
} bind def


% HREPEAT
/HREPEAT % N X Y DX PROC
{
  /PROC exch def
  /DX exch def
  /Y exch def
  /X exch def
  /N exch def
  N { X Y PROC
      X DX add
      /X exch def } repeat
} bind def


gsave

12 /Helvetica FL

gsave

5 100 100 20 { 5 SCIRC } HREPEAT

10 100 200 15 { /Y exch def
                /X exch def
                X Y moveto
                (o) show } HREPEAT

grestore
showpage

Blog Archive

About Me

My photo
Disambiguating biog as there are a few Stephen Tetley's in the world. I'm neither a cage fighter or yachtsman. I studied Fine Art in the nineties (foundation Bradford 1992, degree Cheltenham 1992 - 95) then Computing part-time at Leeds Met graduating in 2003. I'm the Stephen Tetley on Haskell Cafe and Stackoverflow.