Justin Paston-Cooper wrote:
>  What I have is a tally (is that what it's called?) of an array which
>  I'm working on, and I would like to extract this first true element
>  and also I will use the result of firstTrue on the previous tally by
>  combining it with some logical operations.

I'm not quite clear yet; can you describe what you mean by tally?  In J, a
tally normally means to the number of items in an array; its count, as in
"the tally of 'abc' is 3"  or  "tally =: #" [1].

>  I have defined the following:
>  firstTrue =: $ $ ((((+. {.) 0 } ]) , (> {.)) / @ (0 ,~ |.@,) (}. @))

I might define firstTrue as  </\ UnderRavel  as in:

           UnderRavel =: (@,) ($`) ($`) (`:6)

           firstTrue =:  </\ UnderRavel    

           firstTrue _3 ]\ 0 0 1 1 1 0 1 0 0
        0 0 1
        0 0 0
        0 0 0
           
, believing your input is a multidimensional boolean array and your output
has the same shape but is zero everywhere except for a single 1, in the same
place as the very first 1 in the input (ravel-wise).

But I'm not sure that's right; for another thing, your verb does some fancy
stuff and is defined in an unfamiliar way for me (I'm curious why you tacked
(}.@) on at the end, instead of the beginning, where it ends up [2]). 

For another, normally shape and index have meaning to a J array, and rows of
a table (or generally items of an array) correspond to some external
concept, where they are treated as peers and there are no "special" indexes.
Having the items be "instances" of some template is important when you want
to pair them up with other items (for example to tie them back to the
original data), otherwise you'll need to provide special treatment
everywhere (not just in the original verb).  Kind of like a program virus.

Put another way, if it's ravel-index which is important, then what you
really want to work with is the ravel, and the table is just an inconvenient
shape for it [3].  Lose the table.

-Dan


[1]  The monad # is informally known as "tally":
     http://www.jsoftware.com/help/dictionary/d400.htm

[2]  Going through your code reminded me of a treasure I can give you.

      I see you use the pattern }. .... 0 ,~ ....  .  This is a "shift in"
pattern and can be effected directly with a fit of |. , as in

           }. 0 ,~ 1 2 3 4
        2 3 4 0
           
           1 (|.!.0) 1 2 3 4
        2 3 4 0

Use negative rotations for shifting in from the other direction (e.g. _1
|.!.0 ... ), and change the 0 to the fill of your liking.  Works for
non-numeric arrays too (such consistent generalizations make you appreciate
J's design).

[3]  There is only one way I know of in J to access the ravel indexes
directly, without raveling or computation (as in (<@#: i.)@:$ ) , and it's a
sneaky undocumented trick.  It turns out, if the argument to the adverb } is
a _verb_ (not a noun), its output refers to the indices of the _ravel_ of
the array, as in:

           '*' 8:} 4 5 $'.'
        .....
        ...*.
        .....
        .....
           


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

Reply via email to