[Jprogramming] J's equivalent to APL's + <-
Dan Bron
j at bron.us
Tue Oct 30 02:19:33 HKT 2007
Devon wrote:
> mat=. 10 10$0
> ]mat=. (9+ix{mat) ix}mat [ ix=. {(2+i.4);(3+i.6)
You may substitute box (monad < ) for catalog (monad { ) here. That is, you may define ix thus:
ix=. <(2+i.4);(3+i.6)
This is no briefer, but it is "conceptually simpler", because the catalog is a detail hidden from the user, under the covers.
Also, in general, it is better to leverage intrinsic features of primitives, rather than recreate them yourself. As a bonus, primitives often support such usage with special code:
M =: i. 10000 1000
ix =: 300 16 (+ i.)&.> 6000 400
NB. Methods produce identical results
(M {~ < ix) -: (M {~ { ix)
1
NB. But performance differs
] met =. ts&> 'M {~ { ix';'(M {~ < ix)'
0.765355 2.64095e8
0.0197215 1.68131e7
NB. By factors of 40 in time and 15 in space.
'6.2d' 8!:2 %/ met
38.81 15.71
Also, since the logic in primitives is centralized, it is much easier for Roger to optimize it. So even if the two methods had the same performance, if that performance were unacceptably slow, you're much more likely to convince Roger to speed up the primitive than the compound statment (and its potentially myriad cousins). Centralization also implies greater attention, testing, and robustness (and cool stuff like inversion).
-Dan
More information about the Programming
mailing list