Henry wrote:
>Using @ you have
>
> v =: ; @: (<@i."0)
>
>I can do it without @ but I don't think I'd want to.
I guess I don't understand, what's wrong with this?
v =: ; @: (<@:i."0)
As in:
v 3 5 2
0 1 2 0 1 2 3 4 0 1
Of course, if the original definition were:
v0 =: ; @: ( <@(i."0) )
Then changing @ to @: would be meaningful:
v1 =: ; @: ( <@:(i."0) )
v0 3 5 2
0 1 2 0 1 2 3 4 0 1
v1 3 5 2
0 1 2 0 0
0 1 2 3 4
0 1 0 0 0
As a matter of style, I use @: unless I need the result which @ would
provide. This makes my programs longer, but it saves me thinking (the results
of @: are rarely surprising; not so for @ ). Plus, it makes uses of @
stand out, which is good because those points are usually interesting, and
should be given special attention.
My personal favorite example of this is [EMAIL PROTECTED] (real parts of
complex numbers).
-Dan
PS: Kirk Iverson once explained the difference between @ and @: like this:
Whether you use @ or @: reflects how you think about the problem, even if
the verbs derived therefrom are functionally identical. If you think of the
problem as an "assembly line", each item passing from one verb to the nexts,
then use @ . If you think of the problem as "batch processing" where the
complete result of one verb is passed to the next for processing, then use @:
.
This analogy highlights another difference between @ and @: namely
perfomance. In cases where the results are functionally identical, the
performance may not be, simply because of the overhead of initializing the left
verb multiple times. Take the example I gave earlier, the real parts of
complex numbers.
The "assembly line" version, which I tout as a great example of where to use @
, is actually significantly slower than its "batch" analog:
J=: j./ 2 1000 1000 ?@:$ 100
ts '[EMAIL PROTECTED] J'
0.314736 8.38963e6
ts '{."1@:+. J'
0.0325136 2.51668e7
Which is another reason I adopted the "just use @: " rule of thumb. Of
course, that's not to say everyone must. Maybe best practice would be to write
the verb in the way that best reflects how you think about the problem. If you
later notice the performance is unsatisfactory, you can try refactoring (e.g.
switching @: for @ ).
Another tacit is to write the verb the way you think about the problem, but
then take a quick glance through Appendix B of the DoJ [1]. Look for special
code that can be substituted for functionally equivalent, but unoptimized,
parts of your verb. Of course, optimization follows usage, so if you write
some code in a "natural way", but you find that the natural way isn't
optimized, mention it on the Forums. If enough people do the same, eventually
the natural expression will be optimized.
That is, if you think of the solution in terms of @ , don't write it in terms
of @: just because you find it faster. If you do that without mentioning it,
then the @ form will never be optimized, which is potentially a loss for the
language.
[1] Optimized J expressions:
http://www.jsoftware.com/help/dictionary/special.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm