[REBOL] Re: Beginner's automatic type conversion
From: al:bri:xtra at: 23-Sep-2002 12:26
> However, I thought that it would be more flexible to act as if I did not
know in advance what the type of elements was.
Best solution is to use 'load, which will translate the string into rebol
values.
I'd use something like:
map/only Trans func [Block [block!]] [
map/only Block func [String [string!]] [
load String
]
]
My 'Map function looks like (for the regulars, I've made one slight change):
Rebol [
Name: 'Map
Title: "Map"
File: %"Map.r"
Author: "Andrew Martin"
eMail: [Al--Bri--xtra--co--nz]
Web: http://valley.150m.com
Date: 26/August/2002
Version: 1.1.0
Purpose: {Maps or applies the function to all elements of the series.}
Category: [util 1]
Acknowledgements: [
"Joel Neely"
"Ladislav"
]
Example: [
Map func [n [number!]] [n * n] [1 2 3]
;== [1 4 9]
Map [1 2 3] func [n [number!]] [n * n]
;== [1 4 9]
Map [1 2 3 4 5 6] func [a] [print [a]]
;1
;2
;3
;4
;5
;6
;== []
Map [1 2 3 4 5 6] func [a b] [print [a b]]
;1 2
;3 4
;5 6
;== []
Map [1 2 3 4 5 6] func [a b c] [print [a b c]]
;1 2 3
;4 5 6
;== []
]
Requires: %Arguments.r
]
Map: function [
{Maps or applies the function to all elements of the series.}
Arg1 [any-function! series!]
Arg2 [any-function! series!]
/Only "Inserts the result of the function as a series."
][
Result Results Function Series
][
any [
all [
any-function? :Arg1 series? :Arg2
(Function: :Arg1 Series: :Arg2)
]
all [
any-function? :Arg2 series? :Arg1
(Function: :Arg2 Series: :Arg1)
]
throw make error! reduce [
'script 'cannot-use rejoin [
{"} mold 'Map " " mold type? :Arg1 {"}
]
rejoin [
{"} mold type? :Arg2 {"}
]
]
]
Results: make Series length? Series
do compose/deep [
foreach [(Arguments :Function)] Series [
if all [
not unset? set/any 'Result Function (Arguments :Function)
not none? Result
] [
either only [
insert/only tail Results :Result
][
insert tail Results :Result
]
]
]
]
Results
]
> I still struggle a lot to write code like this which executes statements
> it has itself generated and there seems to be so many ways to do the same
thing
> in Rebol that I am never quite sure that I found a good one ;)
Have a look at the lines after:
do compose/deep [
I hope that helps!
Andrew Martin
ICQ: 26227169 http://valley.150m.com/