Here is a function that will break from / based on the first item that would match the condition (v)
breakscan =: 2 : 'u/@:(] [`(>:@] }. [)@.(#@:[ > ]) 1: i:~ v)' the v verb can be anything that builds a boolean list matching the shape of y, and u will be applied to all items to the right of the rightmost true value returned by v. + breakscan (0=]) 1 2 0 3 4 5 0 0 + breakscan (0=]) 1 2 0 3 4 5 12 + breakscan (0=]) 1 2 10 3 4 5 25 + breakscan (0,~ 0=}:) 1 2 0 3 4 5 0 NB. skip check on last item, but then add an extra 0 to boolist 12 the overhead allows about 100M checks per second for (0=]) ----- Original Message ----- From: 'Pascal Jasmin' via Programming <[email protected]> To: "[email protected]" <[email protected]> Cc: Sent: Tuesday, August 26, 2014 12:42:41 AM Subject: Re: [Jprogramming] Existing a Tacit to expand on what you are saying, timespacex '+/`0:@.([: +./ 0 = ]) 0,~ >: i.1e5' 0.00329024 3.28512e6 timespacex '+`0:@.(0 = *)/ 0,~ >: i.1e5' 0.0532716 2.10253e6 the first expression sees if any item is 0, and if not, it does +/ else it returns 0. It is 20 times faster than 2nd expression which checks if either x or y is 0 and then returns 0 There is unfornately no tacit break, but generating an error can let you call the error handler. You could look at the addler32 thread for trying to get an intermediate value of the function up until the error, but: (+`(a:+])@.(0 = *))/ :: 0: 1 4 0 3 2 1 0 timespacex ' (+`(a:+])@.(0 = *))/ :: 0: 0 ,~ >: i.1e5' 0.0021792 3.15366e6 timespacex ' (+`(a:+])@.(0 = *))/ :: 0: 0 , >: i.1e5' 0.0480169 3.15366e6 it turns out that even when the 0 is at the end, and will be the first expression evaluated, the first approach of scanning first and then applying full function is almost as fast. And when the 0 will be caught last, it is just as slow as the check every element as you go approach. It can still be worth doing the latter approach as most functions are more expensive than +, and it could be saving an expensive operation, but in general, for short circuiting, if it is possible to find out where in the data the short circuit will occur, and then just applying the function until the short circuit, that will have the best performance. Still would be nice to have a tacit break. function. ----- Original Message ----- From: Raul Miller <[email protected]> To: Programming forum <[email protected]> Cc: Sent: Monday, August 25, 2014 11:57:57 PM Subject: Re: [Jprogramming] Existing a Tacit The best technique for avoiding work is to not specify it in the first place. @. does work, of course. :: is another option. ^: is another option. Often, though, it's better to instead specify an operation which does the right thing instead of "exiting early". The reason for this has to do with the possibility that more work needs to be done elsewhere in the array. Examples of this kind of "exiting early" include adding 0 and multiplying by 1. Actually, it really depends on the kind of problem you are trying to solve. For example, using i. or indexing could also be considered to be a data driven "exiting early" approach. Usually it's best to focus on the practical issue you are trying to address... Thanks, -- Raul On Mon, Aug 25, 2014 at 11:34 PM, William Szuch <[email protected]> wrote: > What techniques or methods can be used to exit a tacit at multiple points > depending on the results of the data flow through the tacit ?. > > In an explicit this is achieved by using the control statement "return." a > multiple times. > > > > A simple case for one exit would be to use something like: > > > > f1`[email protected] > > > > A more generalised situation: > > > > (fn`[email protected])@:.(f2`extit1 @.t1)@: (f1`[email protected]) > > Regards > > > > Bill Szuch > > > > > > ---------------------------------------------------------------------- > 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 ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
