Howdy Peter, ELan: 

           You're all talking about passing along refinements to
           other functions.  There will be a pretty easy way to do
           it using COMPOSE:

           NOTE -- This won't work on your REBOL yet!!! The
           behavior of COMPOSE has been recently modified to throw
           away UNSETs from PARENs.  This change will be in the
           next upcoming release.

             (Right now, COMPOSE [()] == [unset], but this has
              been changed so COMPOSE [()] = [])

          SO, Given: 

           g: func [/a/b][
               all [a print "A"]
                   all [b print "B"]
           ]

            You can pass along refinements to g like this:

           f: func [/a/b][
               do to-path COMPOSE [g (either a ['a][]) (either b ['b][])]
       ]

             or ... 

           h: func [/a/b][
               foreach i [a b][either none? i [unset i][set i to-lit-word i]]
                   do to-path COMPOSE [g (a) (b)]
       ]


           Not so bad a way to propagate refinements.  The second
           method would probably be more convenient for longer
           sets of refinements.  Of course, you can assemble the
           to-path block to COMPOSE using, what else, COMPOSE!

             On a related note, ( note: the rest of my rant on
             COMPOSE is fully compliant with the current behavior
             of COMPOSE) ... COMPOSE effectively provides Curried
             functions. For instance, given:

                   f: func [parm1 parm2 parm3 parm4][do-something]

                I just want to fill in parm4 and use defaults for
                the others:

                    loop 1 COMPOSE [f 1 2 3 (some-computed-value)]

          Here's another cool use of COMPOSE.  You can create
          local variables very easily:

           vars: [a b c d e f g h i j k]

           g: func COMPOSE [/local (vars)][ do-something ]

           Lets say you have some functions that are very alike
           but only different in a few spots. You can COMPOSE the
           specs and the body and just evaluate sections that are
           different:

           foreach [name op] [my-add + my-sub -][
               set name func [a b] compose [a (op) b]
       ]

           That's a very simple example of what you could do. 

           You can use COMPOSE to generate PARSE rules!

                Often you see people joining strings of code and DOing
                them.  That effectively turns REBOL into TCL, where
                everything is just a string.  That's inefficient and
                sloppy.  REBOL has great symbolic datatypes and
                meta-abilities, and COMPOSE gives you the ability to
                slice and dice REBOL expressions directly!  This makes
                for great leverage and power.

                Just a rant on the marvels of COMPOSE...

                -jeff

                

Reply via email to