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

Newbie question about func refinements

 [1/4] from: edoconnor:g:mail at: 13-Aug-2008 16:14


I'm sure this has been covered elsewhere, but I couldn't find it. Given a function, such as: sum: func ["Return the sum of two numbers." arg1 [number!] "first number" arg2 [number!] "second number" /avg "return the average of two numbers" /sub "subtracts the first from the second" /div "divides the first into the second" ][ switch/default self/refinement [ avg [arg1 + arg2 / 2] sub [arg1 - arg2] div [arg1 / arg2] ][arg1 + arg2] ] Is there a way to dynamically obtain a refinement value so I can use it in a switch statement? In the example above, the pseudo-code expression "self/refinement" is what I'm looking for. Odd that I've never encountered this over the years. I'm sure someone here can set me straight on it. Thanks, Ed

 [2/4] from: santilli:gabriele:gm:ail at: 14-Aug-2008 0:18


On Wed, Aug 13, 2008 at 10:14 PM, Ed O'Connor <edoconnor-gmail.com> wrote:
> Is there a way to dynamically obtain a refinement value so I can use it in a > switch statement? > In the example above, the pseudo-code expression "self/refinement" is what > I'm looking for.
You can't, refinements are not mutually exclusive in general. (Refinements should not change the meaning of the function, just refine its functionality.) You can use CASE though to do something like what you describe rather easily. HTH, Gabriele.

 [3/4] from: tim-johnsons::web::com at: 13-Aug-2008 14:23


On Wednesday 13 August 2008, Ed O'Connor wrote:
> I'm sure this has been covered elsewhere, but I couldn't find it. Given a > function, such as:
<<quoted lines omitted: 17>>
> Odd that I've never encountered this over the years. I'm sure someone here > can set me straight on it.
Terribly blue-collar of me to propose an alternative, but simple solution. Maybe a refinement is not what you want but a "mode" argument Untested code: sum: func[arg1[number!] arge[number!] mode[word! unset!] ;; ...... check for unset? mode and assign default value if necessary. switch/default mode [ 'avg [arg1 + arg2 / 2] 'sub [arg1 - arg2] 'div [arg1 / arg2] ][arg1 + arg2] HTH Tim

 [4/4] from: edoconnor::gmail::com at: 13-Aug-2008 21:15


On Wed, Aug 13, 2008 at 6:18 PM, Gabriele Santilli wrote:
> You can use CASE though to do something like what you describe rather > easily. >
Thanks. I didn't see a listing in the REBOL dictionary or in various other places for CASE, but I now see it in the built-in function help.

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted