RosettaCodeData/Task/First-class-functions/PostScript/first-class-functions.ps

24 lines
704 B
PostScript

% PostScript has 'sin' and 'cos', but not these
/asin { dup dup 1. add exch 1. exch sub mul sqrt atan } def
/acos { dup dup 1. add exch 1. exch sub mul sqrt exch atan } def
/cube { 3 exp } def
/cuberoot { 1. 3. div exp } def
/compose { % f g -> { g f }
[ 3 1 roll exch
% procedures are not executed when encountered directly
% insert an 'exec' after procedures, but not after operators
1 index type /operatortype ne { /exec cvx exch } if
dup type /operatortype ne { /exec cvx } if
] cvx
} def
/funcs [ /sin load /cos load /cube load ] def
/ifuncs [ /asin load /acos load /cuberoot load ] def
0 1 funcs length 1 sub { /i exch def
ifuncs i get funcs i get compose
.5 exch exec ==
} for