[REBOL] Re: A "supercharged" Map function.
From: andrew:martin:colenso:school at: 23-Jul-2003 14:51
Brett wrote:
> One reason I can see is that my function cannot process a series of
words (they get evaluated).
> So how then can one write a map function that does not evaluate the
series elements when the mapping function has multiple arguments - and
preferably
do it fairly concisely? If there isn't a way I may as well stick with
what I have.
Something like this perhaps?
>> a: 1
== 1
>> b: 2
== 2
>> c: 3
== 3
>> d: 4
== 4
>> map [a b c d d c b a] func [x y] [probe reduce [x y]]
[a b]
[c d]
[d c]
[b a]
== [a b c d d c b a]
>> map [a b c d d c b a] func [x y z zz] [probe reduce [x y z zz]]
[a b c d]
[d c b a]
== [a b c d d c b a]
Andrew J Martin
Attendance Officer &
Information Systems Trouble Shooter
Colenso High School
Arnold Street, Napier.
Tel: 64-6-8310180 ext 826
Fax: 64-6-8336759
http://colenso.net/scripts/Wiki.r?AJM
http://www.colenso.school.nz/
(I'm using Rebol to print out my source to avoid Rebol formatting
religious wars...)
>> source map
map: func [
{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 "Doesn't ignore none! values." /local
Result Results Function Series][
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
do compose/deep [
foreach [(Arguments :Function)] Series [
if (
either Full [
compose [not unset? set/any 'Result Function
(Arguments :Function)]
] [
compose/deep [
all [
not unset? set/any 'Result Function
(Arguments :Function)
not none? Result
]
]
]
)
[
(either Only ['insert/only] ['insert]) tail Results
:Result
]
]
]
Results
]
]
>> source Arguments
Arguments: func [
"Returns the arguments of the function as a block."
F [any-function!] /local
Arguments][
Arguments: make block! 2
foreach Argument pick :F 1 [
if refinement? :Argument [
break
]
insert tail Arguments :Argument
]
Arguments
]
DISCLAIMER: Colenso High School and its Board of Trustees is not responsible (or legally
liable) for materials distributed to or acquired from user e-mail accounts. You can report
any
misuse of an e-mail account to our ICT Manager and the complaint will be investigated.
(Misuse can come in many forms, but can be viewed as any material sent/received that
indicate or suggest pornography, unethical or illegal solicitation, racism, sexism, inappropriate
language and/or other issues described in our Acceptable Use Policy.)
All outgoing messages are certified virus-free by McAfee GroupShield Exchange 5.10.285.0
Phone: +64 6 843 5095 or Fax: +64 6 833 6759 or E-mail: [postmaster--colenso--school--nz]