Vocabulary/grave

From J Wiki
Jump to navigation Jump to search

>> <<   Back to: Vocabulary Thru to: Dictionary

u`v u`n m`v m`n Tie or Gerund Conjunction

u`v creates a list of gerunds for u and vnouns that encode the verb values.

A gerund is an atomic box that can be used like any other box and eventually turned back into a verb to be executed.

Grammatically, a gerund is a noun that names the action of a verb, e.g. "Programming is fun".

   calcvolume =: {{               NB. calculate volume, pass to callback given in 4th argument
'l w h callback' =. y
callback`:6 l*w*h                 NB. Turn the gerund into a verb and execute it
}}
   cbverb =: {{echo 'Volume is ' , ": y}}
   calcvolume 1;2;3;<(cbverb`'')  NB. Pass callback verb as a gerund
Volume is 6

Common Uses

1. Combine several verbs into a single value that can be used as an operand to a modifier, which will separate the verbs and use them.

   NB. x v0`v1`v2} y   executes as (x v0 y) (x v1 y)} (x v2 y)
   NB. Here, the first item of x gives the indexes to copy from
   NB. the second item gives the indexes to copy to
   NB. y is an array; the specified portions are copied within the array
   NB. (] {~ 0 {:: [) selects from y using the first element of x
   NB. (1 {:: [) says that the second element of x has the indexes to modify
   NB. ] says to make the change on y
   copywithin =: (] {~ 0 {:: [) ` (1 {:: [) ` ] }
   1 3 copywithin i. 5 5
 0  1  2  3  4
 5  6  7  8  9
10 11 12 13 14
 5  6  7  8  9
20 21 22 23 24

Using the gerund allows three different verbs to inhabit a single value.

2. Create a noun form of a verb so you can use it as an argument to a verb. Evoke Gerund (`:) turns a gerund back into a verb

   NB. x is 3 gerunds; execute one on y, depending on the sign of y
   NB. order of x is 0,+,-
   signexe =: dyad define
((*y) { x) `: 6 y
)
   0:`]`([: smoutput 'negative ' , ":@|) signexe _5
negative 5

3. Select From Agenda (m@.n) and Agent (m@.v) select from the gerund m to pick the verb to be executed.

   (%&2)`(*&3)@.(2&|) 6  NB. Divide by 2 if even, multiply by 3 if odd
3

4. Assign multiple verbs with a single assignment

   '`plus minus div times' =. +`-`%`*   NB. '` names ' =. values
   plus
+
   times
*

5. Most of J's partitioning modifiers support the use of cyclic gerunds.


More Information

1. If an operand is a noun, it is left unchanged. If it is a verb, it is replaced by its gerund form, which is an atomic box. Then the two noun operands are joined as if by Append (,) to create the result of u`v.

It follows that  v0`v1`v2`v3 , which is interpreted left-to-right as ((v0`v1)`v2)`v3 , creates two gerunds for v0 and v1 and then leaves those values unchanged as it first appends the gerund form of v2 and then of v3 .

2. A gerund can be created from a single verb u by tying it with the empty list: u`''.

3. You can "abuse" gerund notation as a neat way of writing a list of boxed strings

   g =: Su`Mo`Tu`We`Th`Fr`Sa
   assert g -: 'Su';'Mo';'Tu';'We';'Th';'Fr';'Sa'

The strings inside the boxes of the list must be valid names, else J signals  domain error . But they need not be the names of actual verbs.

Be aware that when J evaluates a phrase containing an unused name, it assumes for syntax purposes that the name is a verb. This is called "forward declaration".


Details

1. The gerund form of a verb is a special case of atomic representation of values; more generally m and n may be atomic representations of any entities.

2. The forms `n and m` sometimes give a domain error if m/n is boxed and is not a valid gerund.