+:`*:@.(1&p:) i. 20  NB. square the primes, double the nonprimes
0 2 4 9 8 25 12 49 16 18 20 121 24 169 28 30 32 289 36 361

The rank of the result of the selector verb (1&p:) implies the cell-size to be given to the verbs.  For large arguments the cells to be fed into each verb are collected into an array, so the verb must be able to handle such an array of argument cells:

   <`*:@.(1&p:) i. 20  NB. square the primes, double the nonprimes
|rank error, executing monad (<`*:)@.(1&p:)
|each gerund must return a result whose shape agrees with its inputs
|       <`*:@.(1&p:)i.20

< was passed an array of cells, but it didn't return a result for each cell.

Hey, you did say 'efficiently'.

Henry Rich

On 7/13/2023 5:50 PM, Marcin Żołek wrote:
Hello,
I need to define a function that will execute function f on the elements of an 
input array that meet a certain condition, and execute function g on the other 
elements as in the following code in C. How to do this efficiently in J?

for (i = 0; i < N; i++) {
    if (condition(input[i])) {
        output[i] = f(input[i]);
    } else {
        output[i] = g(input[i]);
    }
}

Martin
----------------------------------------------------------------------
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