Early days - I haven't studied the Fold family yet,  but just applying your example

worries me a bit if the following is typical:

   q =: i. 1000000
   ts
6!:2 , 7!:2@]

NB. compare naive (! - ie"old" J) and Fold method to locate match - might be near end of array:

   r[echo ts'r =: 0 *:@[ F:. ([ (1 Z: target&=)) q' [ target =: *:5  NB. near start - better than naive
3.46e_5 6144
0 1 4 9 16 25
   r[echo ts'r=:({.~ (target I.~ *: )  ) q' [ target =: *:5
0.0079057 8.3911e6
0 1 2 3 4

   10{. r[echo ts'r=:({.~ (target I.~ *: )  ) q' [ target =: *:<:#q  NB. at end - worse than naive!?
0.0153818 8.3911e6
0 1 2 3 4 5 6 7 8 9
   10{.r[echo ts'r =: 0 *:@[ F:. ([ (1 Z: target&=)) q' [ target =: *:<:#q
3.54353 8.07812e7
0 1 4 9 16 25 36 49 64 81

I'm thinking of searching a random-ish vector where the match might lie anywhere.

Thanks,

Mike


On 15/01/2020 21:42, Henry Rich wrote:
   0 *:@[ F:. ([ (1 Z: 25&=)) 3 1 4 6 5 7 8 9
9 1 16 36 25

Suggestion for improving Fold are welcomed.

Henry Rich

On 1/15/2020 2:58 PM, 'Sergey Kamenev' via Programming wrote:
Hi!

I need test function f on array x1 x2 ... xn

If f(x) = 0 then no need test another member of array.

I don't understand how write this code loopless.

Main thing: I would avoid of waste computation of f(x) if f(x) = C found.

I think about Fold but I see only ugly decision with using global variables.

Example code for rewriting:

NB. Vector with test values
a =: 1 + i. y
f =: *:

example =: monad : 0
for_i. a do.
 if. 25 = f i do.
   i return.
 end.
0
end.
)

example a

Nice day!
Sergey



----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to