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

[REBOL] Re: A "supercharged" Map function.

From: brett:codeconscious at: 23-Jul-2003 22:54

For amusement, below is a function (built with inspiration from Andrew's function) that creates a mapping function.
>> f: mapfun :add >> f [1 2 3 4]
== [3 7]
>> g: mapfun [to word! append to string! value 1] >> g [a b c d]
== [a1 b1 c1 d1] mapfun: func [ [catch] "Returns a function that maps the specified function onto a series (unsets filtered by default)." mapfunc [any-function! block!] "Function or function body block (use VALUE as argument)." /only "Inserts into result using Only refinement." /filter filterfunc [any-function! block!] "Filter specification." "Single argument function or body (use VALUE as argument)." /local args ] [ if block? :mapfunc [ mapfunc: func [value [any-type!]] mapfunc] if not filter [ filterfunc: [not unset? get/any 'value]] if block? :filterfunc [ filterfunc: func [value [any-type!]] filterfunc] args: head clear any [ find first :mapfunc refinement! tail first :mapfunc] func [series [series!] /local _nfv_ _nfr_] compose/deep [ _nfr_: make type? series length? series foreach [(args)] series [ if filterfunc set/any '_nfv_ (:mapfunc) (args) [ (pick [insert/only insert] found? only) tail _nfr_ get/any '_nfv_ ] ] _nfr_ ] ] Regards, Brett.