Category:Initlib: Difference between revisions
→Library
m (→Library) |
|||
Line 23:
/empty? {dup length 0 eq?}.
/zero? {dup 0 eq?}.
% stack invariant, execute any predicate and leave stack untouched
% except for answer.
/? {
[/.pred] let*
count array astore /.stack exch def▼
/_restore {clear .stack aload pop}.▼
end}.
% stacky functions
Line 80 ⟶ 91:
} loop pop pop]
}.
/all? {
{empty?} ? {pop true exit} if
uncons {?} dip exch not {pop false exit} if
}.
/any? {
{
{empty?} ? {pop false exit} if
uncons {?} dip exch {pop true exit} if
}.
/pipe {
{
{empty?} ? {pop exit} if
uncons {i} dip
}.
% 1 {{2 *} {3 *} {5 *}} pipe
/collect {
{
{empty?} ? {pop exit} if
uncons {?} dip
} loop
}.
% 1 {{2 *} {3 *} {5 *}} collect
% do on all elements of a tree.
Line 97 ⟶ 136:
% ift - allow stack invariant if condition
/ift {
[/.if /.then] let
▲ 4 dict begin
▲ count array astore /.stack exch def
▲ /_restore {clear .stack aload pop}.
▲ .stack aload pop .if {
▲ _restore .then
▲ } {
_restore▼
▲ } ifelse
end}.
% ift - allow stack invariant ifelse condition
/ifte {
[/.if /.then /.else] let
4 dict begin▼
count array astore /.stack exch def▼
▲ } {
▲ } ifelse
end}.
% switch statement.
Line 178 ⟶ 203:
ifte
end}.
/cvstr {
▲ 4 dict begin
/len elements length def
/str len string def
/i 0 def
i len ge { exit } if
str i
%The element of the array, as a hexadecimal string.
%If it exceeds 16#FF, this will fail with a rangecheck.
elements i get cvi
put
/i i 1 add def
} loop
str
end
} def
|