For example:
foo=: >:
X=: 6
}.^:(X ~: f...@{.)^:_ ] 3 1 4 1 5 9
5 9
A better solution, I think, is special code for (f i. 1:)
for more general f, similar to the existing special code
for (= i. 1:), (> i. 1:) etc.
The following timing shows that something different
is going on with (= i. 1:) vs. (x=y)i.1 :
x=: ?1e6$100
6!:2 '1 (= i. 1:) x'
2.26286e_5
6!:2 '(1=x) i. 1'
0.00541857
----- Original Message -----
From: Alexander Mikhailov <[email protected]>
Date: Tuesday, June 8, 2010 23:22
Subject: [Jchat] on the J thought style again
To: [email protected]
> I'm writing a function which goes roughly as the following:
> "from the input array, take first element and do Foo to it. If
> the result is X, stop, otherwise get second element and do Foo
> to it. If the result is X, stop... etc. If the result of last
> element is still not X, return Y"
>
> I started to write like this:
>
> Foo y
>
> and then I realize that I'm doing Foo to all elements of input
> at once, even those which potentially won't be needed.
>
> So, trying to be optimal complicates things. A familiar observation.
>
> What would be an advice to J-ers? Clarity of idea - or, in
> extreme cases, soundness of algorithm? Or, rather, how to find a
> balance here?
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm