[REBOL] Map
From: AJMartin::orcon::net::nz at: 24-Jul-2003 18:06
Spurred on by Brett, I've revised my 'Map and 'Arguments functions.
An example of 'Map's use:
>> map "abcdef" func [c [char!]] [c + 1]
== "bcdefg"
>> map/only/full [1 2 3 4 5 6 7 8] func [i] [if even? i [reduce [i i * 2 i *
3]]]
== [none [2 4 6] none [4 8 12] none [6 12 18] none [8 16 24]]
>> map/only [1 2 3 4 5 6 7 8] func [i] [if even? i [reduce [i i * 2 i * 3]]]
== [[2 4 6] [4 8 12] [6 12 18] [8 16 24]]
>> map [1 2 3 4 5 6 7 8] func [i] [if even? i [reduce [i i * 2 i * 3]]]
== [2 4 6 4 8 12 6 12 18 8 16 24]
Arguments: func [
"Returns the arguments of the function as a block of word! values."
F [any-function!] "The Function"
] [
head clear any [
find first :F refinement!
tail first :F
]
]
Map: function [
{Maps or applies the function to all elements of the series.} [catch]
Arg1 [any-function! series!]
Arg2 [any-function! series!]
/Only "Inserts the result of the function as a series."
/Full "Don't ignore none! values."
][
Function Series Result Results Words
][
throw-on-error [
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
Words: Arguments :Function
do compose/deep [
foreach [(Words)] Series [
if not any [
unset? set/any 'Result Function (Words)
(pick [[none? :Result] []] not Full)
] [
(pick [insert insert/only] not Only) tail Results
:Result
]
]
]
Results
]
]
Andrew J Martin
ICQ: 26227169 http://www.rebol.it/Valley/ http://Valley.150m.com/