# Difference between revisions of "Function composition"

(One intermediate revision by the same user not shown) | |||

Line 12: | Line 12: | ||

[[File:AllCompositions.png|427px]] | [[File:AllCompositions.png|427px]] | ||

− | Of note here is <source lang=apl inline>f⍨∘g⍨</source> which is [[Reverse-compose]] <source lang=apl inline>⍛</source> (also called ''Before''), and the mirrored version of Beside <source lang=apl inline>∘</source> (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]]. | + | Of note here is <source lang=apl inline>f⍨∘g⍨</source> which is equivalent to — although with swapped operands — [[Reverse-compose]] <source lang=apl inline>⍛</source> (also called ''Before''), and the mirrored version of Beside <source lang=apl inline>∘</source> (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 [[swap]]ped: <source lang=apl inline>{(⍵ ⍵⍵ ⍺) ⍺⍺ (⍺ ⍵⍵ ⍵)}</source>. This operator can for example be used to implement [[wikipedia:three-way comparison|three-way comparison]]: | 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 [[swap]]ped: <source lang=apl inline>{(⍵ ⍵⍵ ⍺) ⍺⍺ (⍺ ⍵⍵ ⍵)}</source>. This operator can for example be used to implement [[wikipedia:three-way comparison|three-way comparison]]: | ||

Line 29: | Line 29: | ||

* [[Dyalog '19]]: [https://dyalog.tv/Dyalog19/?v=czWC4tjwzOQ Tacit Techniques with Dyalog version 18.0 Operators] | * [[Dyalog '19]]: [https://dyalog.tv/Dyalog19/?v=czWC4tjwzOQ Tacit Techniques with Dyalog version 18.0 Operators] | ||

* [[Dyalog webinar]]: [https://dyalog.tv/Webinar/?v=Hln3zryunsw Language Features of Dyalog version 18.0 in Depth] | * [[Dyalog webinar]]: [https://dyalog.tv/Webinar/?v=Hln3zryunsw Language Features of Dyalog version 18.0 in Depth] | ||

− | {{APL | + | {{APL features}}[[Category:Composition operators]][[Category:Tacit programming]] |

## Latest revision as of 08:15, 26 April 2022

**Function composition** refers to the "gluing" together of two functions using a dyadic operator such that the functions are applied to the argument(s) as normal, but in a particular pattern specific to the used operator. The term function composition comes from traditional mathematics where it is used for a function when written as . 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. The three main patterns, represented by Atop, Beside, and Over can be visualised as follows:

When any of these are applied monadically, the dotted branch falls away, and they are all equivalent to each other and to of traditional mathematics.

## Additional compositions

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:

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

## External links

- Dyalog '19: Tacit Techniques with Dyalog version 18.0 Operators
- Dyalog webinar: Language Features of Dyalog version 18.0 in Depth

APL features [edit]
| |
---|---|

Built-ins | Primitives (functions, operators) ∙ Quad name |

Array model | Shape ∙ Rank ∙ Depth ∙ Bound ∙ Index (Indexing) ∙ Axis ∙ Ravel ∙ Ravel order ∙ Element ∙ Scalar ∙ Vector ∙ Matrix ∙ Simple scalar ∙ Simple array ∙ Nested array ∙ Cell ∙ Major cell ∙ Subarray ∙ Empty array ∙ Prototype |

Data types | Number (Boolean, Complex number) ∙ Character (String) ∙ Box ∙ Namespace |

Concepts and paradigms | Conformability (Scalar extension, Leading axis agreement) ∙ Scalar function (Pervasion) ∙ Identity element ∙ Complex floor ∙ Total array ordering ∙ Tacit programming (Function composition, Close composition) ∙ Glyph |

Errors | LIMIT ERROR ∙ RANK ERROR ∙ SYNTAX ERROR ∙ DOMAIN ERROR ∙ LENGTH ERROR ∙ INDEX ERROR ∙ VALUE ERROR |