As Marshall once noted, the biggest deficiency in J is looping over an
array when you need a result from each iteration, and the calculation
requires an initial value and some internal state. Your code looks like
result {"_1 f/\. array , initialstate
where each execution of f produces a result value plus the internal
state to feed into the next iteration. The problems are:
* the result is the entire array of internal state, which is more, maybe
MUCH more than you need, since the final result needs only a portion of
the state
* The state is probably not commensurate with a item of the array, so
you end up boxing the initial state and the array items, which is very
wasteful.
I propose a new primitive, call it N. (for insert). N. is an adverb
that produces a conjunction. In (x N.), x specifies options for the
processing, much as the right operand of u;.n does.
Definition:
[x] u (n N.) v y
where
u is the function to be applied
v is the selector to apply to the result of each execution of u, to
produce the part saved in the final result
x is the (optional) initial value (if omitted, f is applied first
between the last 2 items of y)
y is the argument array
n selects from several variants:
bit 0=0 operation goes back to front
bit 0=1 operation goes front to back, as if using &.|.
bit 1=0 selected result from each iteration becomes one item of result
bit 1=1 selected result from each iteration is boxed before becoming
an item of result
Formal definition:
Ndot0 =: 2 : 0
v&{@> u&.>/\. (<"_1 y)
:
v&{@> u&.>/\. (<"_1 y) , <x
)
Ndot1 =: 2 : 0
v&{@> u&.>/\.&.|. (<"_1 y)
:
v&{@> u&.>/\.&.|. (<"_1 y) ,~ <x
)
Ndot2 =: 2 : 0
v&{&.> u&.>/\. (<"_1 y)
:
v&{&.> u&.>/\. (<"_1 y) , <x
)
Ndot3 =: 2 : 0
v&{&.> u&.>/\.&.|. (<"_1 y)
:
v&{&.> u&.>/\.&.|. (<"_1 y) ,~ <x
)
Ndot =: 1 : 0
assert. m e. i. 4
select. m
case. 0 do. Ndot0
case. 1 do. Ndot1
case. 2 do. Ndot2
case. 3 do. Ndot3
end.
)
I look forward to criticism of this proposal.
Henry Rich
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm