Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: Is there a REBOL construct similar to the "map" word as usedin Logo

From: joel::neely::fedex::com at: 28-Jun-2002 15:52

Hi, Gerard, As you've seen, this topic has been kicked around quite a bit... ;-) You might be interested in http://www.escribe.com/internet/rebol/index.html as a general resource for hysterical (ermmm hisTORical) discussions, or http://www.escribe.com/internet/rebol/m13736.html for another variation on the specific MAPLIST theme. Gerard Cote wrote:
> Now is another question relative to the word "map" I already used > in Logo for working on agregates - that is data collections - and > doesn't seem to exist per se in the same form under REBOL. >
One of the reasons that some things we expect from other languages do not appear to be in REBOL is that they're so easy to write for yourself! Yet another small variation is to accept an arbitrary series and return a result of similar type (string versus block): map: func [[throw] f [any-function!] s [series!] /local r] [ r: make block! length? s foreach val s [insert/only tail r f get/any 'val] either any-string? s [ make string! r ][ r ] ]
>> map func [c] [c + 1] "Hello!"
== {Ifmmp"}
>> map func [c] [c + 1] <document>
== "epdvnfou"
>> map func [c] [c + 1] [2 4 6 8]
== [3 5 7 9]
> Here are 2 specific obvious uses of the Logo "map" construct as > I would like it to be in REBOL too : > > ? show map [? * ?] [2 3 4 5] <-- Applies the > product of an item with itself to each member of > [4 9 16 25] the > second list. >
With the above definition, you can do this:
>> map func [n] [n * n] [2 3 4 5]
== [4 9 16 25]
> ? show (map [word ?1 ?2 ?1] [a b c] [d e f]) <--- this means > create words that are combinations of 3 letters > [ada beb cfc] the > first and third ones being taken from the first list while > the second onecomes from the second list. >
Due to differences in handling of arguments between Logo (and LISP) versus REBOL, I'd make that a separate function: mapn: func [[throw] f [any-function!] s [series!] /local n v r] [ n: length? first s foreach ss s [if n > length? ss [n: length? ss]] r: make block! n loop n [ insert/only tail r do join [f] map :first s s: map :next s ] r ] which allows such uses as:
>> mapn :+ [[1 1 1 2 2 2] [1 2 3 10 20 30]]
== [2 3 4 12 22 32] or your example:
>> mapn func [a b] [rejoin [a b a]] [["a" "b" "c"] ["d" "e" "f"]]
== ["ada" "beb" "cfc"] Hope this helps! -jn-