[REBOL] Map
From: al:bri:xtra at: 3-Nov-2000 22:55
Sharriff wrote:
> hate to bother you Andrew, but..
>
> Script: "User Preferences" (9-Oct-2000/13:31:51+1:00)
> >> help map
> No information on map (word has no value)
>
> ????
> map next first system func [word] [write join %system- word mold in system
word]
> Could you tell me what map does?
I could. Here's a practical example from Spider, my web server. It's of the
'post function, which doesn't do much at the moment, merely returning a
form's POST-ed data. Here's the function. Note the 'map and 'list:
Post: func [Request [block!]] [
Respond/Header/Entity OK [Content-Type "text/html"] reduce [
HTML [
title "Request"
body
h1 "Request"
list map Request func [Text] [
pre Text
]
]
]
]
Which generates the following nice html (other tags snipped for clarity):
<h1>Request</h1>
<ul>
<li><pre>POST /MyTest HTTP/1.1</pre>
</li>
<li><pre>Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/pdf, */*</pre>
</li>
<li><pre>Referer: http://localhost:8080/Form.html</pre>
</li>
<li><pre>Accept-Language: en-nz</pre>
</li>
<li><pre>Content-Type: application/x-www-form-urlencoded</pre>
</li>
<li><pre>Accept-Encoding: gzip, deflate</pre>
</li>
<li><pre>User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)</pre>
</li>
<li><pre>Host: localhost:8080</pre>
</li>
<li><pre>Content-Length: 54</pre>
</li>
<li><pre>Connection: Keep-Alive</pre>
</li>
<li><pre>name=Andrew+Martin&sex=M&income=%2450%2C001+and+higher</pre>
</li>
</ul>
My version of 'map follows my .sig.
Andrew Martin
ICQ: 26227169
http://members.nbci.com/AndrewMartin/
-><-
[
Rebol [
Name: 'Map
Title: "Map"
File: %"Map.r"
Home: http://members.nbci.com/AndrewMartin/Rebol/Enhancements/
Author: "Andrew Martin"
eMail: [Al--Bri--xtra--co--nz]
Date: 30/October/2000
Version: 1.1.0
History: [
1.1.0 "Added /Only refinement."
1.0.0 "Original."
]
Enhancement: 'Map
Acknowledgements: [
"Joel Neely"
"Ladislav"
]
Purpose: {
Map: Maps a function to all elements of a block.
}
Example: [
Map func [n [number!]] [n * n] [1 2 3]
Map [1 2 3] func [n [number!]] [n * n]
]
]
Map: function [
"Maps a function to all elements of a block."
[throw catch]
Arg1 [any-function! block!]
Arg2 [any-function! block!]
/Only "Inserts the result of the function as a series."
][
Results Function Block
][
throw-on-error [
any [
all [
any-function? :Arg1 block? :Arg2
(Function: :Arg1 Block: :arg2 true)
]
all [
any-function? :Arg2 block? :Arg1
(Function: :Arg2 Block: :arg1 true)
]
throw make error! reduce [
'script 'cannot-use rejoin [
{"} mold 'Map " " mold type? :Arg1 {"}
]
rejoin [
{"} mold type? :Arg2 {"}
]
]
]
Results: make block! length? Block
either only [
foreach Item Block [
insert/only tail :Results Function :Item
]
][
foreach Item Block [
insert tail :Results Function :Item
]
]
Results
]
]
]