2009/11/18

PostScript 配列を扱う手続き

 

%!PS-Adobe-3.0
% P.38 2.12.4 配列を扱う手続き

/reverse {
    5 dict begin
        /ary exch def
        /n a1 length def
        /ret n array def
        0 1 n 1 sub {
            /i exch def
            /e ary i get def
            ret n i sub 1 sub e put
        } for
        ret
    end
} def

[1 2 3 4 5] reverse ==

%!PS-Adobe-3.0
% P.39 2.12.4 配列を扱う手続き
% sum array elements

/suma {
    2 dict begin
        /ary exch def
        /ret 0 def
        ary {
            /ret exch ret add def
        } forall
        ret
    end
} def

[1 2 3 4 5 6 7 8 9 10] suma == % 55

%!PS-Adobe-3.0
% P.40 2.12.4 配列を扱う手続き

/amap {
    6 dict begin
        /f exch def
        /ary exch def
        /n ary length def
        /ret n array def
        0 1 n 1 sub {
            /i exch def
            /e ary i get def
            ret i e f put
        } for
        ret
    end
} def

[1 2 3 4 5] { 10 mul } amap ==
% [10 20 30 40 50]
[true false false true] { { 1 } { 0  } ifelse } amap ==
% [1 0 0 1]

0 件のコメント:

コメントを投稿