Function composition

From APL Wiki
Revision as of 21:10, 24 June 2020 by Adám Brudzewsky (talk | contribs) (Created page with "'''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 norma...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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 the 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:

Compositions.png

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 {⍵⍵ ⍺ ⍺⍺ ⍵}.

Of note is Reverse-compose (also called Before), which is 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 operands swapped result: {(⍵ ⍵⍵ ⍺) ⍺⍺ (⍺ ⍵⍵ ⍵)}. 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

External links

APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentityStopSelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndexCartesian ProductSort
Selector Index generatorGradeIndex OfInterval IndexIndicesDealPrefix and suffix vectors
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)
Quad names Index originComparison toleranceMigration levelAtomic vector