Compose

From NARS2000
Jump to navigationJump to search

In the following descriptions, f and g represent functions and a and b represent variables.

  • The form fg may be used both monadically and dyadically.
Monadic: Z←fg R is identical to Z←fg R.
Dyadic: Z←L fg R is identical to Z←L fg R.
  • The form f∘b may be used monadically only.
Monadic: Z←(fb) R is identical to Z←R f b.
Note that parentheses are required around the function to avoid interpreting b R as a strand.
  • The form ag may be used monadically only, unless g is any of /, , /[X], \, , or \[X].
Monadic: Z←ag R is identical to Z←a g R.
Dyadic: Z←L (a∘/) R (and its related variants and /[X]) is called Mask (see below).
Dyadic: Z←L (a∘\) R (and its related variants and \[X]) is called Mesh (see below).
  • The derived function from the form ab always signals a SYNTAX ERROR.

Compose can be useful for function assignment (but enclosing parentheses are necessary).

For example

      p1←(1∘+¯2∘π) ⍝ 1 more than the Nth prime number
      p1 ⍳9
3 4 6 8 12 14 18 20 24

Mask

Z←L (a∘/) R
Z←L (a∘⌿) R
Z←L (a∘/[X]) R
each returns some items in order but interleaved from the left and right arguments, possibly repeated multiple times and possibly omitting values from both arguments, combined into a single result, according to the values in a.
L is an array of arbitrary rank.
a is a scalar or vector of integers.
R is an array of arbitrary rank.
[X] is the optional Axis along which the function is applied — if this option is omitted, the function is applied along the rightmost axis.
Either or both of L and R can be scalars in which case the scalar is reshaped to the shape of the other argument. Otherwise, the two arguments must be of the same rank and shape.
After L and R have been scalar extended, if a is a scalar, it is extended to be the same length as the common coordinate in the arguments. Otherwise, a must be a vector of the same length as (⍴L)[X] (or, equivalently, (⍴R)[X]).


The function Mask selects some items from the left and right arguments depending upon the Left Operand (a). For vector arguments, if a[I] is negative, it selects the Ith item from L; if it is positive it selects the Ith item from R; if it is zero, the Ith items from L and R are ignored. The absolute value of a[I] indicates how many copies of the selected item are placed into the result.

For this function, the position in a (e.g., first, second, third, ...) indicates which item of the arguments (e.g., first, second, third, ...) is selected. The sign of the integer in a indicates whether it's the left argument (negative), right argument (positive), or neither (zero).

For example,

      'ABC' (¯1 3 ¯2∘/) 'abc'
AbbbCC
      (3 3⍴⎕A) (¯1 3 ¯2∘/) 3 3⍴⎕a
AbbbCC
DeeeFF
GhhhII
      (3 3⍴⎕A) (¯1 3 ¯2∘⌿) 3 3⍴⎕a
ABC
def
def
def
GHI
GHI
      (3 3⍴⎕A) (¯1 0 ¯2∘⌿) 3 3⍴⎕a
ABC
GHI
GHI
      (3 3⍴⎕A) (¯1 0 ¯2∘/) 3 3⍴⎕a
ACC
DFF
GII

This function is essentially equivalent to

(|LO~0)/[X] (⊂(⍋×LO,-LO)[⍋⍋×LO~0])⌷[X] L,[X] R

where LO is the Left Operand a.

Mesh

Z←L (a∘\) R
Z←L (a∘⍀) R
Z←L (a∘\[X]) R
each returns all items in order but interleaved from the left and right arguments, possibly repeated multiple times and possibly including the right argument's fill element, combined into a single result, according to the values in a.
L is an array of arbitrary rank.
a is a scalar or vector of integers.
R is an array of arbitrary rank.
[X] is the optional Axis along which the function is applied — if this option is omitted, the function is applied along the rightmost axis.
If L and R are both vectors, their lengths are related to the values in a and the result Z as follows:

⍴L ←→ +/a<0 ⍴R ←→ +/a>0 ⍴Z ←→ +/1⌈|a

If either L or R is a scalar, it is reshaped to the shape of the other argument, except for the implicit or explicit Axis dimension whose length follows the above rules for vectors. Otherwise, the two arguments must have identical shape except for the Axis dimension whose length follows the above rules for vectors.


The function Mesh selects all items from the left and right arguments depending upon the Left Operand (a). For vector arguments, the values in the Left Operand are organized into the negative, zero, and positive values. As the above rules indicate, the length of the left argument is equal to the number of negative values in the Left Operand, and the length of the right argument is equal to the number of positive values. This means that every element of the left and right arguments appears in the result. In particular, the Ith item from the left argument appears in the result corresponding to the position of the Ith negative item in a, and the Ith item from the right argument appears in the result corresponding to the position of the Ith positive item in a. The absolute value of the item from the Left Operand indicates how many times the corresponding item from the chosen argument is repeated in the result. A zero in the Left Operand causes the right argument's fill element to appear in the result.

For example, 'I' (1 ¯1 2 ¯1 2 ¯1 2 ¯1 ∘\) 'MSSP' ←→ 'IIII' (1 ¯1 2 ¯1 2 ¯1 2 ¯1 ∘\) 'MSSP' because there are four negative items in the Left Operand.

      'I' (1 ¯1 2 ¯1 2 ¯1 2 ¯1 ∘\) 'MSSP'
MISSISSIPPI
      'BIB'  (¯1 ¯1 1 ¯1 1 0 1 1 2 1 1 1 ∘\) 'LOBAGINS'
BILBO BAGGINS
      (3 3⍴⎕A) (1 ¯1 2 ¯2 3 ¯3∘\[2]) 3 3⍴⎕a
aAbbBBcccCCC
dDeeEEfffFFF
gGhhHHiiiIII
      (3 3⍴⎕A) (1 ¯1 2 ¯2 3 ¯3∘⍀) 3 3⍴⎕a
abc
ABC
def
def
DEF
DEF
ghi
ghi
ghi
GHI
GHI
GHI

This function is essentially equivalent to

(|LO)\[X] (⊂⍋⍋×LO~0)⌷[X] L,[X] R

where LO is the Left Operand a. Note that this definition is dependent upon extending the Expand function to (Signed) Integer Left Arguments as in

      2 ¯2 1 0 2\'ABC'
AA  B CC