Hi Kenneth,

2009/10/28 Lettow, Kenneth <[email protected]>:
> The equivalent phrase is:
> CO=: ({...@] <: [)*.([ < {:@])
> A quick test shows that it is faster and almost as lean.
> ...

Thank you for pointing out this, and strange that CO is so
much faster.  Would anybody knowledgeable explain why?

As for ‘lean’ … well, CO is a direct expression of what is wanted
(y1<=x and x<y2), so nothing to be surprised of.  Plus, it needs
operations for explicitly (albeit tacitly) accessing each of x, y1
and y2, including extracting y1 and y2 from a vector.  I wouldn't
– in an array language – call this lean :)  In fact, it would have
been more straightforward to express the same in C.

The first variant I came up was

   w1=. (>:{.)*.(<{:)

which is not unlike CO but simpler.  Then I had

   w2=. (<{.)<(<{:)

which is simpler yet.  I was finally satisfied with

   w3=. </@:<

Each of w1, w2, and w3 is simpler than CO, and w1 and w2
are as fast.  Only w3 is slower, for a reason that is mysterious
to me.

I should also add that, in case it is not known which end of the
interval is the lower one, we could use a very similar verb:

   w4=. ~:/@:<

in place of w3.

   2.4 (~:/@:<) 2 3
1
   2.4 (~:/@:<) 3 2
1
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to