I wrote:
> See http://www.jsoftware.com/svn/DanBron/trunk/environment/strand.ijs
> See,
> http://www.jsoftware.com/svn/DanBron/trunk/environment/parameterized_verbs.ijs
> .
Just for kicks, I combined the features of these two scripts so that you
can define verbs like this:
NB. Can define my own arguments and their default values
myFunc =: verb fdef
filename = 'c:\short.log'
max = 42
color = 'red'
)
NB. Note no reference to x or y ... just using
NB. my arguments as "expected" or "normal".
smoutput 'Logging to: ', filename
smoutput 'Max is: ' , ": max
NB. Can determine which of my arguments was user-specifed
NB. and which defaulted, and do something about it.
if. color_is_default do.
smoutput 'My favorite color is ' ,color
else.
smoutput 'Your favorite color is ',color
end.
)
and then call them like this:
myFunc[max=906, color=blue fs]
Logging to: c:\short.log
Max is: 906
Your favorite color is blue
or any number of other ways:
NB. Strand notation
myFunc['c:\file.txt' 906 'blue' fs]
NB. Commas, like other langs
myFunc['c:\file.txt', 906, 'blue' fs]
NB. Unspecified args are defaulted ("optional")
myFunc['c:\file.txt' fs]
NB. Can use named arguments, like eg VB
myFunc[color='blue' fs]
NB. Often values needn't be quoted
myFunc[color= blue fs]
NB. Combination of comma syntax and name=value
myFunc[max=906, color=blue fs]
NB. Spelling of names is flexible
myFunc[MAX=906, COLOR=blue fs]
NB. Can use other delimiters, too...
myFunc<MAX=906, COLOR=blue fs>
etc.
And just to be clear: this is *not* what Neville is asking for. But I hope
it lays to rest the question of whether J functions can support more than
2 arguments. The syntax above is similar to many other languages', and
should be familiar to most "mainstream" programmers.
In fact, it is more flexible than the syntax of many other languages (e.g.
it supports the VB-like name=value syntax, which C, Java, etc do not).
As an extension of that, it would be easy to leverage strand notation to
for other specialized arguments to verbs. For example, one could write
something like:
sql [select FOO where BAR > (max-99) and BAZ e. 'abc' fs]
There are some limitations of course, but this comes pretty close to a
top-level DSL in J. See
http://www.jsoftware.com/svn/DanBron/trunk/environment/calling_convention.ijs
for details.
-Dan
PS: It's pretty easy to extend the parameterized_verb script to do other
useful things with parameters. For example, I combined it with memory
mapped files to produce lexical closures over names (like the 'static'
keyword in VB):
http://www.jsoftware.com/svn/DanBron/trunk/uncategorized/lexicalclosure.ijs
And it would be similarly easy to extend to, for another example,
argument-checking: the parameter prolog to the function could indicate the
expected type, rank, or arbitrary other characteristics of each input.
None of this is to say these ideas would be terribly useful in J; this
email is more of a response to the spirit that "J's calling conventions
are unsophisticated" which sometimes crops up on these Forums.
This email is demonstrates that J can (mostly) emulate the calling features
of other languages. But for the most part we don't need to, or want to:
J's intrinsic calling conventions not only suffice, but are most often
superior in the context of J programming (especially given our focus on
brevity).
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm