Reverse Compose: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
(monadic: hook)
m (correct definition of monadic Reverse Compose as (f Y) g Y)
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Built-in|Reverse Compose|⍛}}, also known as '''Before''', is a [[primitive operator]] closely related to [[Beside]] (<source lang=apl inline>∘</source>), also known as ''After''. Called [[dyad|dyadically]] with function operands <source lang=apl inline>f</source> and <source lang=apl inline>g</source>, it uses <source lang=apl inline>f</source> [[monad|monadically]] to pre-processes the left argument before applying <source lang=apl inline>g</source> between the pre-processed left argument and the given right argument. <source lang=apl inline>X f⍛g Y</source> is thus equivalent to <source lang=apl inline>(f X) g Y</source>. The operator can be defined as the [[dop]] <source lang=apl inline>{(⍺⍺ ⍺) ⍵⍵ ⍵}</source>.
{{Built-in|Reverse Compose|⍛}} or '''Behind''' is a [[primitive operator]] closely related to [[Beside]] (<syntaxhighlight lang=apl inline>∘</syntaxhighlight>), which appears in [[Extended Dyalog APL]] and [[dzaima/APL]]. Called [[dyad|dyadically]] with function operands <syntaxhighlight lang=apl inline>f</syntaxhighlight> and <syntaxhighlight lang=apl inline>g</syntaxhighlight>, it uses <syntaxhighlight lang=apl inline>f</syntaxhighlight> [[monad|monadically]] to pre-processes the left argument before applying <syntaxhighlight lang=apl inline>g</syntaxhighlight> between the pre-processed left argument and the given right argument. <syntaxhighlight lang=apl inline>X f⍛g Y</syntaxhighlight> is thus equivalent to <syntaxhighlight lang=apl inline>(f X) g Y</syntaxhighlight>. The operator can be defined as the [[dop]] <syntaxhighlight lang=apl inline>{(⍺⍺ ⍺) ⍵⍵ ⍵}</syntaxhighlight>. This dyadic definition matches the [[hook]] function Before, represented as <code>⊸</code> in [[BQN]].


Reverse compose was introduced in [[Extended Dyalog APL]], and then adopted into [[dzaima/APL]]. Its [[dyadic]] case matches [[I]]'s Hook (<code>h</code>), which is a reflected form of a [[J]] [[Hook]], while Backhook (<code>H</code>) matches the ordinary [[Hook]] or [[Compose]]: because I's [[precedence]] order is left to right, it considers the "reversed" APL form to be primary. The [[monadic]] case was discussed for a while in the [[APL Orchard]]. To be completely consistent with the other [[compositional operators|function composition]], the left "leg" would be removed, making <source lang=apl inline>f⍛g Y</source> be equivalent to <source lang=apl inline>g Y</source>. However, this would never apply <source lang=apl inline>f</source> and would questionably be called a ''composition'' of <source lang=apl inline>f</source> and <source lang=apl inline>g</source>. Instead, it was concluded that the best solution would be to have <source lang=apl inline>f⍛g Y</source> be a [[hook]], <source lang=apl inline>f⍛g⍨Y</source>, that is, <source lang=apl inline>f⍛g</source>{{←→}}<source lang=apl inline>f⍛g⍨⍨</source>.
Unlike Before, the [[monad]]ic case of Reverse Compose has differed across implementations. When introduced by [[Extended Dyalog APL]], <syntaxhighlight lang=apl inline>f⍛g Y</syntaxhighlight> evaluated to <syntaxhighlight lang=apl inline>g Y</syntaxhighlight>, but the later Dyalog APL Vision defines<ref>[[Adám Brudzewsky|Brudzewsky, Adám]]. Dyalog APL Vision. [https://github.com/abrudz/dyalog_vision/blob/main/JotUnderbar.aplo JotUnderbar].</ref> it to be <syntaxhighlight lang=apl inline>(f Y) g Y</syntaxhighlight>, matching Before. This later definition might also be written <syntaxhighlight lang=apl inline>f⍛g</syntaxhighlight>{{←→}}<syntaxhighlight lang=apl inline>f⍛g⍨⍨</syntaxhighlight>{{←→}}<syntaxhighlight lang=apl inline>g⍨∘f⍨</syntaxhighlight>. In [[dzaima/APL]] the monadic case is simply an error.


== Common usage ==
== Common usage ==
Its plain usage is to pre-process left arguments without needing one or more applications of Commute (<source lang=apl inline>⍨</source>). For example, the square of the left argument minus the right argument can be expressed as:
Its plain usage is to pre-process left arguments without needing one or more applications of Commute (<syntaxhighlight lang=apl inline>⍨</syntaxhighlight>). For example, the square of the left argument minus the right argument can be expressed as:


[https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn//1Hf1EdtE4wPT3/Uu@JR72xdo///AQ Try it online!]<source lang=apl>
[https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn//1Hf1EdtE4wPT3/Uu@JR72xdo///AQ Try it online!]<syntaxhighlight lang=apl>
       3×⍨⍛-2
       3×⍨⍛-2
7
7
</source>{{Works in|[[dzaima/APL]], [[Extended Dyalog APL]]}}
</syntaxhighlight>{{Works in|[[dzaima/APL]], [[Extended Dyalog APL]]}}


It can also be combined with Beside to create the [[split-compose]] construct. Here, we take the [[sign]] of the left argument and apply it to (that is, multiply it with) the absolute value of the right argument:
It can also be combined with Beside to create the [[split-compose]] construct. Here, we take the [[sign]] of the left argument and apply it to (that is, multiply it with) the absolute value of the right argument:
[https://tio.run/##SyzI0U2pSszMTfz//1Hf1EdtE4wVDq03VDA5PP1R72wg0TGj5tB6I6CYuYLh//8A Try it online!]<source lang=apl>
[https://tio.run/##SyzI0U2pSszMTfz//1Hf1EdtE4wVDq03VDA5PP1R72wg0TGj5tB6I6CYuYLh//8A Try it online!]<syntaxhighlight lang=apl>
       3 ¯1 4×⍛×∘|¯2 ¯7 1
       3 ¯1 4×⍛×∘|¯2 ¯7 1
2 ¯7 1
2 ¯7 1
</source>{{Works in|[[dzaima/APL]], [[Extended Dyalog APL]]}}
</syntaxhighlight>{{Works in|[[dzaima/APL]], [[Extended Dyalog APL]]}}
== External links ==
=== Documentation ===
* [https://mlochbaum.github.io/BQN/doc/hook.html BQN] (as <code>⊸</code>)
=== Publications ===
* [https://github.com/abrudz/primitives/blob/main/behind.aplf APL model]
 
== References ==
<references/>
{{APL built-ins}}[[Category:Primitive operators]][[Category:Composition operators]]
{{APL built-ins}}[[Category:Primitive operators]][[Category:Composition operators]]

Revision as of 20:43, 24 October 2022

Reverse Compose () or Behind is a primitive operator closely related to Beside (), which appears in Extended Dyalog APL and dzaima/APL. Called dyadically with function operands f and g, it uses f monadically to pre-processes the left argument before applying g between the pre-processed left argument and the given right argument. X f⍛g Y is thus equivalent to (f X) g Y. The operator can be defined as the dop {(⍺⍺ ⍺) ⍵⍵ ⍵}. This dyadic definition matches the hook function Before, represented as in BQN.

Unlike Before, the monadic case of Reverse Compose has differed across implementations. When introduced by Extended Dyalog APL, f⍛g Y evaluated to g Y, but the later Dyalog APL Vision defines[1] it to be (f Y) g Y, matching Before. This later definition might also be written f⍛g f⍛g⍨⍨ g⍨∘f⍨. In dzaima/APL the monadic case is simply an error.

Common usage

Its plain usage is to pre-process left arguments without needing one or more applications of Commute (). For example, the square of the left argument minus the right argument can be expressed as:

Try it online!

      3×⍨⍛-2
7

It can also be combined with Beside to create the split-compose construct. Here, we take the sign of the left argument and apply it to (that is, multiply it with) the absolute value of the right argument:

Try it online!

      3 ¯1 4×⍛×∘|¯2 ¯7 1
2 ¯7 1

External links

Documentation

Publications

References

  1. Brudzewsky, Adám. Dyalog APL Vision. JotUnderbar.
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