from vocabulary definition of for.
(quote)
for. T do. B end.
for_xyz. T do. B end.
The B block is evaluated once for each item of the array A that results from
evaluating the T block. In the for_xyz. form, the local name xyz is set to
the value of an item on each evaluation of the B block and xyz_index is set
to the index of the item; results are not dependable if these names are
re-assigned within the B block.
(end-quote)
1. This seems to imply that a variable called xyz_index is created and
updated
automatically, but I cannot get that to work so assume that is not the case.
2. The implementation of the for. and for_xyz. blocks seem excessively over-
complicated for what they actually do, and in certain cases take up far more
space than I would have expected for what it actually achieves:
testfor =: 3 :0
sum=:0
for_j. >:i.y. do.
sum=:sum+j
end.
sum
)
testfor 10000
50005000
ts 'testfor 10000'
0.0581344835726 133888
ts 'testfor 100000'
0.612705525423 1051392
3. On the other hand, the while. construct seems easy to understand and
use, takes up far less space, but also takes a lot longer to run! Is there a
happy medium?
testwhile =: 3 :0
j=:0
sum=:0
while. j<y. do.
j=:>:j
sum=:sum+j
end.
sum
)
testwhile 10000
50005000
ts 'testwhile 10000'
0.0984287109116 2112
ts 'testwhile 100000'
1.02252884096 2112
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm