Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: Is there a REBOL construct similar to the "map" word as used in Logo

From: al:bri:xtra at: 28-Jun-2002 16:28

This 'Map function might be suitable for your requirements. Thanks again to Joel and Ladislav! [ Rebol [ Name: 'Map Title: "Map" File: %"Map.r" Author: "Andrew Martin" eMail: [Al--Bri--xtra--co--nz] Date: 18/Oct/2001 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 ;== [] ] 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 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://valley.150m.com/ -><- ----- Original Message ----- From: "Gerard Cote" <[gerardcote--sympatico--ca]> To: <[rebol-list--rebol--com]> Sent: Friday, June 28, 2002 2:50 PM Subject: [REBOL] Is there a REBOL construct similar to the "map" word as used in Logo ?