There are a number of recognized constructions that will provide what you're looking for: >From the dictionary page on special code<http://www.jsoftware.com/help/dictionary/special.htm>
f i.0: dyad special code for the following functions; see the J 5.01<http://www.jsoftware.com/help/release/compsc.htm>, J 5.03 <http://www.jsoftware.com/help/release/compsc1.htm>, and J 5.04 release notes <http://www.jsoftware.com/help/release/edot504.htm>. f i. 0: i.&0@:f first place where not x f y f i. 1: i.&1@:f first place wherex f y f i: 0: i:&0@:f last place where not x f y f i: 1: i:&1@:f last place where x f y [: + / f + /@:f number of places where x f y [: +./ f +./@:f x f y anywhere? [: *./ f *./@:f x f y everywhere? [: I. f I. @:f indices where x f y Notes: • f is one of the verbs = ~: < <: > >: E. e. • Special code is available only for Boolean, integer, floating point, literal, and symbol arguments • Special code is used only for atoms and lists if f is not e. • If f is E. , special code only for E.i.1: +/@:E. +./@:E. I.@ :E. On Sat, Jul 6, 2013 at 7:31 PM, Alexander Mikhailov <[email protected]>wrote: > > > How to find first element of array which satisfies the given predicate? > > I'm asking because I wonder how to implement it efficiently. An array > could be very long, say a billion elements, and chances are the first > element satisfying a certain predicate may almost certainly be found in > first, say, ten elements. > > This is not about a specific problem, rather I bump into this question > every once in a while. > > Alexander > > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm > ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
