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

[REBOL] Is there a REBOL construct similar to the "map" word as used in Logo ?

From: gerardcote:sympatico:ca at: 27-Jun-2002 22:50

Hello, First I'd like to thank everybody that helped me elucidate my previous question about the use of the SET word for doing multiple assignments from 2 blocks, specially Joel for the detailed explanation he furnished to me. 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. May be I don't really need it for solving the following case but nevertheless I would like to get some advice about something similar in REBOL if anyone exists that is different than the two ones I found myself when using the "forall" and "foreach" words. Here is an example of what I am just trying to get when using the Logo "map" construct : Extract the initials of a complete name by getting the first letter from each of the first and the last names contained in a block. Using the REBOL syntax this could be done with the following statement :
>> print initials [George Harrison]
[G H] Under Logo, the "map" statement is used to work on complete data collections by using a single statement that shows a very concise syntax - from which stems my interest for this "map" word. For example to extract the initials above I can put "map" to work in a function named "initials" as this : to initials :name <-- Here "to" plays the same role as the "func" word under REBOL output map "first :name <-- "output" simply ends the function and returns the resulting value end to the current context A detailed explanation of how the function operates appears below. After the "initials" function has been defined, the user of the program can call it with the command that follows after the ? Logo prompt below : ? show initials [George Harrison] "show" is similar to the print in REBOL. [G H] <--- Results returned when "initials" is evaluated. This simply defines a function named "initials" that accepts an input parameter called "name" the body itself is trying to display something that the "map" statement applies to each member of the input parameter "name". Here this "something" is the action represented by the lit-word "first". It actually takes the first letter of each word contained in the list submitted as a parameter to the initials function and return all of them in a block enclosed in square brackets. I know that I can use "foreach" and "forall" to solve this problem (look below for my solutions) but can somebody tell me if there is a REBOL word able to follow very closely the syntax used by the Logo "map" construct ? I already tried the following alternatives that all seem to work as expected for my previous problem :
>>my-sentence: [this is a simple sentence]
== [this is a simple sentence]
>> foreach word my-sentence [print first to-string word]
t i a s s
>> forall my-sentence [print first to-string my-sentence]
t i a s s The sole reason I plan to look further in the direction of a REBOL "map" clone is that I want to experiment some other concept that looks very promising and seems more powerful than what I actually know. The man who is the lead developer for the UCB-Logo I am also playing with while learning REBOL - Mr. Brian Harvey - names this promising and powerful concept a "template-based iteration". Other functional languages also frequently use the term "anonymous function" for something that looks a bit related to the question-marked items in the following examples but it seems that's all there is to it, as far as I know of. In fact I already found under REBOL some ways to achieve results similar to those offered by the Logo "map" construct. But the actual syntax I use is longer and has a different form than the one I look for (maybe I am only dreaming awake... with the hope to get something that don't really exists under REBOL !!!). Whatever the case, efficiently achieving the same results would probably require to use the PARSE word but I am just curious to know if someone is aware about the existence of any current REBOL construct for achieving similar results for the simple cases provided below. This REBOL construct - if it really exists - would completely eliminate the need for me to write even the simplest parsing rules for such trivial cases. 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. ? 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. Thank you in advance for any information relative to such a construct if any. Regards, Gerard P.S. If nobody has heard of a REBOL clone for this "map" construct, I plan to write one myself since I want to experiment with these concepts under REBOL exactly as it is possible to do so with Logo. If I am succesful with the implementation and I find this is really unavoidable to work without it, I'll share it with everybody interested by this.