Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.postscript > #2081

map and higher order functions

From Carlos <angus@quovadis.com.ar>
Newsgroups comp.lang.postscript
Subject map and higher order functions
Date 2014-10-30 17:27 +0100
Organization A noiseless patient Spider
Message-ID <20141030172728.0983c6bc@samara.DOMA> (permalink)

Show all headers | View raw


How would you implement map and other higher order functions
(procedures taking one or more procedures)?

Especially the part when the procedure is to be run, and you must clear
the stacks from your working elements.

I've resorted to use a stack of dictionaries anchored on userdict
(implemented with a linked list) to keep local variables. It works but
I don't really like it.

What are other possibilities?

Carlos.

PS: what I'm using now:

#!
%----8<-----

userdict begin

/$--libstack-- null def

% - -frame- <current frame>
/-frame- { userdict /$--libstack-- get } bind def

% <dict> -newframe- -
/-newframe- {
    dup /$--next-- userdict /$--libstack-- get put
    userdict /$--libstack-- 3 -1 roll put
} bind def

% - -endframe- -
/-endframe- {
    userdict /$--libstack--
        userdict /$--libstack-- get  /$--next-- get
    put
} bind def

end % userdict
    

% applies proc to each element of array/str,
% collects results into new array/str
% <array/string> <proc> map <new array/string>
/map {
    5 dict begin
        /proc exch def
        /arr  exch def
        /res  arr length
              arr type /stringtype eq { string } { array } ifelse
        def
        /i    0   def
        currentdict
    end -newframe-
    0 1 -frame- /arr get length 1 sub { % for
        -frame- /i 3 -1 roll put
        -frame- /arr get  -frame- /i get get
        -frame- /proc get exec
        -frame- /res get  -frame- /i get  3 -1 roll put
    } for
    -frame- /res get
    -endframe-
} bind def

%----8<-----

-- 

Back to comp.lang.postscript | Previous | NextNext in thread | Find similar


Thread

map and higher order functions Carlos <angus@quovadis.com.ar> - 2014-10-30 17:27 +0100
  Re: map and higher order functions ken <ken@spamcop.net> - 2014-10-30 16:57 +0000
    Re: map and higher order functions Carlos <angus@quovadis.com.ar> - 2014-10-30 19:55 +0100
      Re: map and higher order functions ken <ken@spamcop.net> - 2014-10-31 12:17 +0000
  Re: map and higher order functions Ross Presser <rpresser@gmail.com> - 2014-10-30 20:30 -0700
    Re: map and higher order functions Carlos <angus@quovadis.com.ar> - 2014-11-01 23:58 +0100
  Re: map and higher order functions luser- -droog <mijoryx@yahoo.com> - 2014-10-30 22:56 -0700
    Re: map and higher order functions Carlos <angus@quovadis.com.ar> - 2014-11-02 00:15 +0100
      Re: map and higher order functions luser- -droog <mijoryx@yahoo.com> - 2014-11-15 00:43 -0800
        Re: map and higher order functions Carlos <angus@quovadis.com.ar> - 2014-11-16 01:37 +0100
          Re: map and higher order functions luser- -droog <mijoryx@yahoo.com> - 2014-11-21 00:17 -0800
          Re: map and higher order functions luser- -droog <mijoryx@yahoo.com> - 2015-02-27 00:59 -0800

csiph-web