Category:Initlib: Difference between revisions
Improved display of source code, enabled syntax highlighting, and fixed sloppy formatting and typos.
(Improved display of source code, enabled syntax highlighting, and fixed sloppy formatting and typos.) |
|||
(4 intermediate revisions by 2 users not shown) | |||
Line 1:
Here is a library of functions tested in
<syntaxhighlight lang="shell" inline>
ghostscript -q -dNODISPLAY -c '(initlib.ps) run'
</syntaxhighlight>
== <code>initlib.ps</code> ==
<
%!PS
% conventions: parameter names with let begins with .
Line 23:
/empty? {dup length 0 eq?}.
/zero? {dup 0 eq?}.
% stack invariant, execute any predicate and
% leave stack untouched except for answer.
/? {
4 dict begin
end}.
% stacky functions
Line 64:
/uncons {getname getbody}.
/concat {exch [ rup aload pop counttomark -1 roll aload pop ] }.
% make a unit list.
/unit {1 array astore cvx}.
Line 81 ⟶ 82:
/find {
4 dict begin
end}.
/transpose {
[ exch {
{ {empty? exch pop} map all?} {pop exit} ift
[ exch {} {uncons {exch cons} dip exch} fold counttomark 1 roll] uncons
} loop ] {reverse} map
}.
/zip {[rup] transpose}.
% [[1 2 3 4 ] [5 6 7 8] [9 10 11 12]] transpose
/all? {
{
}.
% 1 {{10 gt} {5 gt} {0 gt}} any?
/any? {
{
}.
/pipe {
{
}.
% 1 {{2 *} {3 *} {5 *}} pipe
/collect {
{
}.
% 1 {{2 *} {3 *} {5 *}} collect
Line 123 ⟶ 127:
/treemap {
[/.tree /.rec] let
end}.
Line 137 ⟶ 141:
/ift {
[/.if /.then] let
end}.
% ift - allow stack invariant ifelse condition
/ifte {
[/.if /.then /.else] let
end}.
% switch statement.
Line 151 ⟶ 155:
/dip {
[/.v /.q] let
end}.
/apply {exec}.
Line 158 ⟶ 162:
/linrec {
[/.if /.then /.rec1 /.rec2] let
end}.
/binrec {
[/.if /.then /.rec1 /.rec2] let
end}.
/genrec {
[/.if /.then /.rec1 /.rec2] let
end}.
Line 188 ⟶ 192:
/primrec {
5 dict begin
[/.param /.then /.rec] let*
end}.
/cvstr {
{
%
%
end
} def
Line 227 ⟶ 231:
/puts {= (\n) print flush}.
/, {(=============\n)
% set the prompt to something else so that we know initlib is loaded.
/prompt {(>| ) print flush} bind def</syntaxhighlight>
|