Philop Viton asked:
> Any suggestions?
Well, if took input as an Nx2 array of boxes, where {."1 y were the names
(conforming to J naming conventions) and {:"1 y were the values, you could
start your verbs off this way:
({."1 y)=. {:"1 y
Once, long ago, I wrote some utility code to automate this, so that you can
write code like this, for example:
parameterizedVerb =: verb defn
filename = 'c:\short.log'
max = 42
color = 'red'
)
smoutput LF,'======='
smoutput 'Logging to: ', filename
smoutput 'Max is: ' , ": max
if. color_is_default do.
smoutput 'My favorite color is ',color
else.
smoutput 'Your favorite color is ',color
end.
smoutput '======='
)
Note the 'verb defn' as opposed to the usual 'verb define'. The text following
verb defn gives the names and default values of the parameters. So
parameterizedVerb has 3 parameters, with names filename max color and
default values 'c:\short.log' 42 'red' respectively.
The callers of parameterizedVerb have several options. The first is that
Nx2 table of boxes I mentioned:
parameterizedVerb 3 2 $ 'filename';'c:\long.log'; 'max';100;
'color';'blue'
=======
Logging to: c:\long.log
Max is: 100
Your favorite color is blue
=======
Of course the named params can be given in any order. Extending that idea, any
or all of the parameters may be omitted; omitted params will be assigned their
default values (and omitted_param_is_default will be 1 [as opposed to
specified_param_is_default which will be 0 ]).
parameterizedVerb 3 2 $ 'max';100; 'filename';'c:\long.log';
'color';'blue'
=======
Logging to: c:\long.log
Max is: 100
Your favorite color is blue
=======
parameterizedVerb 3 2 $ 'max';100; 'filename';'c:\long.log'
=======
Logging to: c:\long.log
Max is: 100
My favorite color is red
=======
Callers also don't need to care about parameter capitalization or other
formatting details:
parameterizedVerb 1 2 $ 'max';100
=======
Logging to: c:\short.log
Max is: 100
My favorite color is red
=======
parameterizedVerb 1 2 $ 'MAX';100
=======
Logging to: c:\short.log
Max is: 100
My favorite color is red
=======
If the list of parameters will only ever grow, and new parameters can always be
given useful defaults, then your callers may opt to call with a simple list of
boxes, just giving the values of the parameters in order (as in 'a b c'=.y
today).
parameterizedVerb 'c:\long.log';100;'blue'
=======
Logging to: c:\long.log
Max is: 100
Your favorite color is blue
=======
In this case, only trailing parameters can be omitted (when (#y) < #parameters
).
parameterizedVerb 'c:\long.log';100
=======
Logging to: c:\long.log
Max is: 100
My favorite color is red
=======
parameterizedVerb '' NB. Use all the defaults
=======
Logging to: c:\short.log
Max is: 42
My favorite color is red
=======
I hadn't touched this code in years, but for the sake of this thread I pulled
it out into its own script and tweaked it to work in J6. I haven't reviewed it
otherwise. You may get it here:
http://www.jsoftware.com/svn/DanBron/trunk/environment/parameterized_verbs.ijs
.
-Dan
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm