Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder1.news.weretis.net!feeder4.news.weretis.net!border2.nntp.ams2.giganews.com!backlog4.nntp.ams3.giganews.com!backlog4.nntp.ams.giganews.com!Xl.tags.giganews.com!border2.nntp.ams.giganews.com!nntp.giganews.com!local2.nntp.ams.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail NNTP-Posting-Date: Thu, 30 Oct 2014 11:57:36 -0500 From: ken Newsgroups: comp.lang.postscript Subject: Re: map and higher order functions Date: Thu, 30 Oct 2014 16:57:26 -0000 Message-ID: References: <20141030172728.0983c6bc@samara.DOMA> Reply-To: ken@spamcop.net MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit User-Agent: MicroPlanet-Gravity/3.0.4 X-Antivirus: avast! (VPS 141030-1, 30/10/2014), Outbound message X-Antivirus-Status: Clean Lines: 47 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-UCn71sKz0SGmcFd+EvQ2greRVQgXSz8WBE6dP3lgzwqlWUCWO3gCnsvV0mLini8ftsht5znIBk6r4hU!knnPBX/7GACjFbvfJaPLlFJkIwNkvbXGUiogYM7tcRUrgcEg0K2o6dgzpSDrEN7KoJWk3Ag//5A4!15uiY1B13itI9w== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2622 Xref: csiph.com comp.lang.postscript:2082 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 > % map 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