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


Groups > comp.lang.postscript > #2082

Re: map and higher order functions

From ken <ken@spamcop.net>
Newsgroups comp.lang.postscript
Subject Re: map and higher order functions
Date 2014-10-30 16:57 +0000
Message-ID <MPG.2ebc7e4067cb96d19898cf@usenet.plus.net> (permalink)
References <20141030172728.0983c6bc@samara.DOMA>

Show all headers | View raw


In article <20141030172728.0983c6bc@samara.DOMA>, angus@quovadis.com.ar 
says...
> 
> How would you implement map and other higher order functions
> (procedures taking one or more procedures)?

I'm not sure what your 'map' function is intended to do, but...
 

> % applies proc to each element of array/str,

forall applies 'proc' to each element in turn of arrays. packedarrays, 
dictionaries or strings, essentially any compound object in PostScript.

So I think I'd use forall myself.


> % collects results into new array/str
> % <array/string> <proc> map <new array/string>

If you want to retrieve results, then you'll have to have proc store the 
results *somewhere* as it executes. You could create a local variable, 
stash it in userdict, or simply create a variable on the stack before 
you execute forall, and have proc use 'index' to take a copy of it, 
which it can then manipulate:

Eg
(returned string) (string to process) 

{
1 index    %% Working variable is now top of stack
exch       %% (working string) string element 'n'
....
....       %% do some processing
....
} 

forall

This would leave (returned string) on the stack at the end, obviously 
your procedure has to be careful about stack manipulations.

NB if the local variable needs to grow, you might need to reallocate it 
and that would mean removing the copy from the stack, and replacing it 
with the increased allocation variable.

		Ken

Back to comp.lang.postscript | Previous | NextPrevious in thread | Next 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