# Function composition

Function composition refers to the "gluing" together of two or more functions using a dyadic operator or a train such that the functions are applied to the argument(s) as normal, but in a particular pattern. The term function composition comes from traditional mathematics where it is used for a function $h(x)=f(g(x))$ when written as $h(x)=(f\circ g)(x)$ . APL generalises this idea to dyadic functions, allowing various patterns of application in addition to the simple application of one monadic function to the result of another monadic function.

## Common compositions

Reverse Compose and Beside treat their arguments in an asymmetric way. They can be seen as using a monadic function to pre-process the left or right argument, respectively, before proceeding to apply a main function. Their patterns can be visualised as follows:

Atop and Over treat their arguments in a symmetric way. They can be seen as using a monadic function to post-process the result or pre-process the argument(s) of a main function. Their patterns can be visualised as follows:

Trains provide a way to apply a function to the result(s) of one (for 2-trains) or two (for 3-trains) other functions. These patterns are also called Atop and Fork, and can be visualised as follows:

The operator represented by Jot (`∘`, in this context called Bind) and the 3-train can also be used with constant arrays, then treating the arrays (`A`) as constant functions, much as if they were used as operands to the Constant operator (`A⍨`):

### Summary of rules

The above compositions can be summarised as follows:

`  (f⍛g  ) ⍵` $\Leftrightarrow$ `(  f ⍵) g      ⍵ `
`⍺ (f⍛g  ) ⍵` $\Leftrightarrow$ `(  f ⍺) g      ⍵ `
`  (  g∘h) ⍵` $\Leftrightarrow$ `        g (  h ⍵)`
`⍺ (  g∘h) ⍵` $\Leftrightarrow$ `   ⍺    g (  h ⍵)`

`  (  g⍤h) ⍵` $\Leftrightarrow$ `        g (  h ⍵)`
`⍺ (  g⍤h) ⍵` $\Leftrightarrow$ `        g (⍺ h ⍵)`
`  (  g⍥h) ⍵` $\Leftrightarrow$ `        g (  h ⍵)`
`⍺ (  g⍥h) ⍵` $\Leftrightarrow$ `(  h ⍺) g (  h ⍵)`

`  (  g h) ⍵` $\Leftrightarrow$ `        g (  h ⍵)`
`⍺ (  g h) ⍵` $\Leftrightarrow$ `        g (⍺ h ⍵)`
`  (f g h) ⍵` $\Leftrightarrow$ `(  f ⍵) g (  h ⍵)`
`⍺ (f g h) ⍵` $\Leftrightarrow$ `(⍺ f ⍵) g (⍺ h ⍵)`

`  (A∘g  ) ⍵` $\Leftrightarrow$ `   A    g      ⍵ `
`  (  g∘A) ⍵` $\Leftrightarrow$ `   ⍵    g      A `
`  (A g h) ⍵` $\Leftrightarrow$ `   A    g (  h ⍵)`
`⍺ (A g h) ⍵` $\Leftrightarrow$ `   A    g (⍺ h ⍵)`

Additional compositions are possible, even without using an argument more than once or applying a function to its own result. However, most of these are rather trivial shuffled-around versions of the above three. For example, one could define an operator identical to Atop, only that it applies the right operand to the result of the left operand, that is `{⍵⍵ ⍺ ⍺⍺ ⍵}`.

When Dyalog added Atop and Over, it was with the reasoning that these were the only compositions where the leftmost function acted as the "root" function in the evaluation tree, while the arguments were used each on their respective sides of the constituent functions:

Of note here is `f⍨∘g⍨` which is equivalent to — although with swapped operands — Reverse-compose `⍛` (also called Before), and the mirrored version of Beside `∘` (also known as Compose and After), because it is the only such variation that has been implemented, namely in dzaima/APL and Extended Dyalog APL.

A compositional operator that isn't just a shuffled around version of the basic three, is one that applies one operand between the other operand's dyadic result and the result of that other operand's result when swapped: `{(⍵ ⍵⍵ ⍺) ⍺⍺ (⍺ ⍵⍵ ⍵)}`. This operator can for example be used to implement three-way comparison:

Try it online!

```      S ← {(⍵ ⍵⍵ ⍺) ⍺⍺ (⍺ ⍵⍵ ⍵)}
cmp ← -S≤
2 cmp 3
¯1
3 cmp 3
0
4 cmp 3
1
```