Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

"G" a golf library, version 0: 3 easy pieces

27 views
Skip to first unread message

luser- -droog

unread,
Jan 30, 2014, 2:59:22 AM1/30/14
to
Compiling some experiments for enhancing PostScript's
competitiveness for code-golf, here's a rough-draft,
"Version 0" of a ps-golfing library. For lightest-weight,
the file should be named simply 'G' with no extension.
(G)run %7 chars buys you . $ @ # for accessing operators.
(G)run A%8 chars also gives you abbreviated names.
(G)run A D%10 chars starts a block of implicit procedures
(actually executable strings, to get technical)
which will be named A,B,C.... up to the next blank line.

There are some long lines with trailing comments here,
careful that your news channel doesn't break them.

-droogie

%!
%G
%ps golfing utilities collection
%
%eg.
% %!
% (G)run A % load abbreviation dict,
% D % begin implicit procs
%
%Version 0

%!
%operators by code number or number-strings
%by dynamically constructing a binary token in a string

/.{<920>dup 1 4 3 roll put cvx exec}def % execute operator by table number
/${//. 73 .}def %73== /forall % execute each char in string as operator number
/@{{32 sub //. exec}73 .}def % ' '->0
/#{{95 add //. exec}73 .}def % ' '->127

%!
%implicit procedure definitions
%-library- version

%D begins an implicit-procedure block,
%defining the next line as A, the next as B, ...
%according to ascii sequence, until a blank line.
/D{[(A){
currentfile 99 string readline not { pop exit } if
dup length 0 eq { exit } if
cvx
1 index 1 string copy dup dup 0 get 1 add 0 exch put
}loop>>begin}def

%!
%abbr.ps
% abbreviated system names
% exports (defines) 1 procedure:
% - ABBR dict
% which generates an abbreviation dictionary.
%
% This allows you to use alphabetic abbreviations for
% standard systemdict names, suxh as:
%
% a %= abs
% ad %= add
% mu %= mul
% pr %= print
% g %= ge
% f %= file
% etc. Check PLRM 2ed or 3ed, Appendix F Encoded System Names
% Edit for compilation: name ABBR has been shortened to A
% and dict is left on dictstack.

/A {
2 dict begin
225 -1 0 { % generate defs in reverse alphabetic (numeric) order
%dup =only( )=only
<910> dup 1 4 3 roll put cvx exec
%dup =only( )=only
cvx dup =string cvs % op str
%pstack()= stop
exch
dup { currentdict end exch load
} stopped { pop begin cvlit }{ exch begin exch pop } ifelse
exch
%pstack() = %stop
0
1 1 3 index length
%1 sub
{ % opname str 0 i
3 copy getinterval % opname str 0 i str[0,i]
%dup =only( )=only
cvn 4 index def
pop % opname str 0
} for
%()=
pop pop pop
} for
0 1 225 { % print out the shortest ABBR name for each system name.
dup =only( )=only
<910> dup 1 4 3 roll put cvx exec % opname
dup =only( )=only
cvx dup =string cvs
exch
dup { currentdict end exch load
} stopped { pop begin cvlit }{ exch begin exch pop } ifelse
exch
0
{
1 1 3 index length 1 sub {
3 copy getinterval
dup cvn load % opname str 0 i str[0,i] loaded
5 index eq {
=only( )=only
pop
stop
}{
pop pop
} ifelse
} for

} stopped pop
()=
pop pop pop
} for
/ABBR where pop /ABBR currentdict put %memo
%currentdict end
} bind def

luser- -droog

unread,
Jan 30, 2014, 3:07:51 AM1/30/14
to
On Thursday, January 30, 2014 1:59:22 AM UTC-6, luser- -droog wrote:

> the file should be named simply 'G' with no extension.
> (G)run %7 chars buys you . $ @ # for accessing operators.
> (G)run A%8 chars also gives you abbreviated names.
> (G)run A D%10 chars starts a block of implicit procedures
> (actually executable strings, to get technical)
> which will be named A,B,C.... up to the next blank line.

Be advised! immediately after Z comes [ \ ], so you maybe
don't want too many implicit procedures, or you lose some
functionality.

luser- -droog

unread,
Jan 30, 2014, 3:09:58 AM1/30/14
to
Damn! I forgot to suppress the output from the abbreviation
algorithm, so the "A" option is a little useless in this
version. :(

luser- -droog

unread,
Jan 30, 2014, 3:49:07 AM1/30/14
to
Updated. Executes this 152-char Sierpinski Triangle

(G)run A D
{A - B + A + B - A}
{B B}

{A - B - B}7{[ex{du w{(>K?\2u)$}if}fora]}rep
cvx[/A{3 0 rl}/B 1 in/-{120 rot}/+{-120 rot}>>b
100 200(k?B9)$ showp


Et voila:


%!
%G
%ps golfing utilities collection
%
%eg.
% %!
% (G)run A % load abbreviation dict,
% D % begin implicit procs
%
%Version 0a
%0a bugfixes. remove output from ABBR construction
<910> dup 1 4 3 roll put cvx exec % opname
%dup =only( )=only
cvx dup =string cvs
exch
dup { currentdict end exch load
} stopped { pop begin cvlit }{ exch begin exch pop } ifelse
exch
0
{
1 1 3 index length 1 sub {
3 copy getinterval
dup cvn load % opname str 0 i str[0,i] loaded
5 index eq {
pop%=only( )=only
pop
stop
}{
pop pop
} ifelse
} for

} stopped pop
%()=
pop pop pop
} for
/A where pop /A currentdict put %memo
%currentdict end
} bind def

0 new messages