Here is a quick example with numbers:

   list =. /:~ ?.@$~ 100
   item =. 19
   {: ((,~{.)`(,{:)@.(item > list{~[)~ <.@-:@+/)^:_ ]0,#list
23
   list I. item
23
   I =: 4 :'{: ((,~{.)`(,{:)@.(y > x{~[)~ <.@-:@+/)^:_ ]0,#x'
   list I item
23
   (list I. i.100) -: (list I"_ 0 i.100)
1

However, symbols don't compare in the same way, so a binary search isn't
applicable here.

   </~ t
0 0 0 0 0 0
1 0 1 1 0 0
1 0 0 0 0 0
1 0 1 0 0 0
1 1 1 1 0 0
1 1 1 1 1 0

Depending on what you are doing, you could re-engineer the items that you
use or just use regular indexing with i. .

Marshall

On Mon, Aug 29, 2011 at 5:03 AM, Steven Taylor <[email protected]> wrote:

> I was looking for an example of power being used for a binary search.  Is
> there an example of this in a wiki or a list post?
>
> So far I've ended up with the below, which works well, but originally I was
> aiming at doing a binary search using symbols.  Probably there is a
> crossover point where one outperforms the other.  I don't need this to be
> super fast right now, but probably it's a good thing to know.
>
> t=.s:'`zero`one`two`three`four`five'
>
> {. I. ; t =/ s:'`three'   N.B. take first incase of duplicates.  Also, s:
> returns a one item list, hence the need for /
>
> thanks,
> -Steven
> ----------------------------------------------------------------------
> 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