Pascal Jasmin wrote:
>       NB. update only selected items from y with monad
> function of y, leave rest unchanged.
> upfltr =: 4 : 0
>       NB. update filtered y. x updateV`fltrV, 
>       '`up f' =. x
>       (up (I. f y){ y) (I. f y) } y
> )
> NB. double items greater than 10:
>    +:`(10&>) upfltr 5 9 11 12
> 10 18 11 12
> 
> is there a faster way to implement the above?

Well... in general, generality should be focused on application
requirements.

With that in mind, here's how I'd implement 
"double items greater than 10":

   digt10=: * 2 ^ 10 < ]

Looking closer, that's not what your sample code did.
If, instead, I wanted to implement "double items less
than 10", I'd use

   dilt10=: * 2 ^ 10 > ]

Now, I understand that the specific sentences I've proposed here
are not "fully general".  However, I've expressed them in J, and
J is fully general.

More generally, J gives us a boolean data type which lets me 
express the results of arbitrary tests, and it gives me a 
variety of verbs which have Boolean data type in their domain.

> Seems to me the above verb should be one of the built-in
> gerund ammend options, (maybe it is, and I just don't
> understand the documentation for it).  

It's possible to use amend here, but amend uses indices as
an intermediate data type, which requires (at minimum) an
extra step to convert test results from boolean to index.

I suspect I'd rather lobby for "arithmetic-like" selection 
operations on non-numeric data (which are based on the 
concept of an identity element in those domains).

Anyways, here's a generic pattern for using amend with a
selection operation.

First, a statement of the problem:

 Given selection operation S and function B, and list L
 produce list L2 where, if L were numeric,
    L2 -: ((S L)*B L) + (1-S L)*L

Second, some solutions:

Using amend (assuming B is an appropriate function):
   L2=: (B (S L)#L) (I. S L)} L

Or
   L2=: 0 ([: B [EMAIL PROTECTED] # ])`([: I. [EMAIL PROTECTED])`]} L
(I wish the three gerund form of } worked without that garbage 
left argument.  But the dictionary explicitly states that without 
the garbage left argument the first gerund is ignored.)

Or
   L2=: (([: B S # ]) [`([: I. [EMAIL PROTECTED])`]} ]) L

Or, more simply:
   L2=: (([: B S # ]) ([: I. [EMAIL PROTECTED])} ]) L

Of course, even with non-numeric data, we have other options,
such as using power:
   L2=: B^:S"_1 L
or
   L2=: (B [^:([EMAIL PROTECTED])"_1 ]) L

Finally, note that I've not bothered to introduce any
names here.  I tend to prefer using names that are
relevant to my application.

-- 
Raul

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

Reply via email to