[REBOL] Re: Maplist?
From: al:bri:xtra at: 9-Sep-2001 10:54
You could try:
[
Rebol [
Name: 'Map
Title: "Map"
File: %"Map.r"
Home: http://zen.scripterz.org/Rebol/Enhancements/
Author: "Andrew Martin"
eMail: [Al--Bri--xtra--co--nz]
Date: 17/November/2000
Version: 1.5.0
History: [
1.5.0 {Removed 'throw-on-error to make it easier to track bugs down.}
1.4.0 {Modified to have any number of arguments per function}
1.3.0 {Modified to work with series! instead of block!.}
1.2.0 {unset! results are not returned - so allowing values to be filtered
out.}
1.1.0 "Added /Only refinement."
1.0.0 "Original."
]
Enhancement: 'Map
Acknowledgements: [
"Joel Neely"
"Ladislav"
]
Purpose: {Maps or applies the function to all elements of the series.}
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
;== []
]
]
Arguments: function [f [any-function!]] [Arguments] [
Arguments: make block! 2
foreach Argument pick :f 1 [
if refinement? :Argument [
break
]
append Arguments :Argument
]
Arguments
]
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 true)
]
all [
any-function? :Arg2 series? :Arg1
(Function: :Arg2 Series: :Arg1 true)
]
throw make error! reduce [
'script 'cannot-use rejoin [
{"} mold 'Map " " mold type? :Arg1 {"}
]
rejoin [
{"} mold type? :Arg2 {"}
]
]
]
Results: make Series length? Series
do reduce [
'foreach Arguments :Function 'Series compose [
if not unset? set/any 'Result Function (Arguments :Function) [
either only [
insert/only tail Results :Result
][
insert tail Results :Result
]
]
]
]
Results
]
]
Andrew Martin
ICQ: 26227169 http://zen.scripterz.org
-><-
----- Original Message -----
From: "David Ness" <[DNess--Home--Com]>
To: "Rebol List" <[rebol-list--rebol--com]>
Sent: Sunday, September 09, 2001 8:54 AM
Subject: [REBOL] Maplist?
> I've look thru the documentation, but haven't had the answer to a very
> simple question jump out at me yet.
>
> Question:
> What converts ["123" "45667"] to [123 45667]
> or generalizing:
> How do I generate the block that is the result of applying some
> function to each element of an existing block (the classical
> Lisp `Maplist') function;
> perhaps more importantly,
> what documentation should I look at that would point me in the
> direction to solutions to this class of problems...
> --
> To unsubscribe from this list, please send an email to
> [rebol-request--rebol--com] with "unsubscribe" in the
> subject, without the quotes.
>
-- Attached file included as plaintext by Listar --
-- File: Map.r
[
Rebol [
Name: 'Map
Title: "Map"
File: %"Map.r"
Home: http://zen.scripterz.org/Rebol/Enhancements/
Author: "Andrew Martin"
eMail: [Al--Bri--xtra--co--nz]
Date: 17/November/2000
Version: 1.5.0
History: [
1.5.0 {Removed 'throw-on-error to make it easier to track bugs down.}
1.4.0 {Modified to have any number of arguments per function}
1.3.0 {Modified to work with series! instead of block!.}
1.2.0 {unset! results are not returned - so allowing values to be filtered out.}
1.1.0 "Added /Only refinement."
1.0.0 "Original."
]
Enhancement: 'Map
Acknowledgements: [
"Joel Neely"
"Ladislav"
]
Purpose: {Maps or applies the function to all elements of the series.}
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
;== []
]
]
Arguments: function [f [any-function!]] [Arguments] [
Arguments: make block! 2
foreach Argument pick :f 1 [
if refinement? :Argument [
break
]
append Arguments :Argument
]
Arguments
]
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 true)
]
all [
any-function? :Arg2 series? :Arg1
(Function: :Arg2 Series: :Arg1 true)
]
throw make error! reduce [
'script 'cannot-use rejoin [
{"} mold 'Map " " mold type? :Arg1 {"}
]
rejoin [
{"} mold type? :Arg2 {"}
]
]
]
Results: make Series length? Series
do reduce [
'foreach Arguments :Function 'Series compose [
if not unset? set/any 'Result Function (Arguments :Function) [
either only [
insert/only tail Results :Result
][
insert tail Results :Result
]
]
]
]
Results
]
]