j

Building an array of verbs in J


Is it possible to build arrays of verbs? I've tried this:

f =: >:
f2 =: f f

There's no syntax error but f2 is clearly not an array of verbs. For instance

f2 yields f f
$ f2 yields $ f2
0 { f2 yields 0 { f2

2 3 $ f behaves in a similar way.

I'd also like to know if verbs can be activated by the name.

Edited

Instead of f2 =: f f, which is a composition, let's have

f =: >:
f2 =: 2 1 $ f

f2 yields 2 1 $ f
$ f2 yields $ f2
0 { f2 yields 0 { f2

It seems that f2 represents the sequence 2 1 $ f, while in the case of an atom, e.g. 2 1 $ 7, the right side is a vector.


Solution

  • A collection of functions is called a gerund in J and is formed using the tick "`":

    g =: +`-`f
    ┌─┬─┬─┐
    │+│-│f│
    └─┴─┴─┘
    

    You can use @. to apply the appropriate verb using its index:

    4 2 3 (g @. 0) 1 5 6
    5 7 9
    4 2 3 (g @. 1) 1 5 6
    3 _3 _3
    4 2 3 (g @. 2) 1 5 6
    1 0 0
    
    NB. defining a 2x2 gerund:
    k =: 2 2 $ +`-`*`%
    ┌─┬─┐
    │+│-│
    ├─┼─┤
    │*│%│
    └─┴─┘
    
    1 (((<0 0) { k) @. 0) 2
    3
    1 (((<0 1) { k) @. 0) 2
    _1
    1 (((<1 0) { k) @. 0) 2
    2
    1 (((<1 1) { k) @. 0) 2
    0.5
    

    The index can also be a new verb that depends on the arguments or a list of indices.