Category:Initlib: Difference between revisions
Improved display of source code, enabled syntax highlighting, and fixed sloppy formatting and typos.
(transpose) |
(Improved display of source code, enabled syntax highlighting, and fixed sloppy formatting and typos.) |
||
(3 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 {
}.
/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 130 ⟶ 127:
/treemap {
[/.tree /.rec] let
end}.
Line 144 ⟶ 141:
/ift {
[/.if /.then] let
end}.
% ift - allow stack invariant ifelse condition
/ifte {
[/.if /.then /.else] let
end}.
% switch statement.
Line 158 ⟶ 155:
/dip {
[/.v /.q] let
end}.
/apply {exec}.
Line 165 ⟶ 162:
/linrec {
[/.if /.then /.rec1 /.rec2] let
end}.
/binrec {
[/.if /.then /.rec1 /.rec2] let
end}.
/genrec {
[/.if /.then /.rec1 /.rec2] let
end}.
Line 195 ⟶ 192:
/primrec {
5 dict begin
[/.param /.then /.rec] let*
end}.
/cvstr {
{
%
%
end
} def
Line 234 ⟶ 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>
|