Commute: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
m (Added fork emulation)
(Definition in SHARP and inclusion in Extended APL standard)
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Built-in|Commute|⍨}} is a [[monadic operator]] that takes a [[dyadic function]] as operand and modifies how the argument(s) of its derived functions are used. If the derived function is used monadically, the argument is also used as left argument. This usage is also known as '''Self''' or, more informally, '''Selfie'''. If the derived function is used dyadically, the arguments are swapped. This usage is also known as '''Swap'''. The [[glyph]] is also used for [[Commute]].
{{Built-in|Commute|⍨}}, or <syntaxhighlight lang=apl inline>⊂</syntaxhighlight> in [[SHARP APL]], is a [[monadic operator]] that takes a [[dyadic function]] as operand and modifies how the argument(s) of its derived functions are used. If the derived function is used monadically, the argument is also used as left argument. This usage is also known as '''Self''' or, more informally, '''Selfie'''. If the derived function is used dyadically, the arguments are swapped. This usage is also known as '''Swap'''. The [[glyph]] is also used for [[Constant]].


== Examples ==
== Examples ==


This decrements from the vector:
This decrements from the vector:
<source lang=apl>
<syntaxhighlight lang=apl>
       1 -⍨ 1 2 3
       1 -⍨ 1 2 3
0 1 2
0 1 2
</source>
</syntaxhighlight>
{{Works in|[[Dyalog APL]], [[dzaima/APL]], [[NARS2000]]}}
{{Works in|[[Dyalog APL]], [[dzaima/APL]], [[NARS2000]]}}
Double:
Double:
<source lang=apl>
<syntaxhighlight lang=apl>
       +⍨1 2 3
       +⍨1 2 3
2 4 6
2 4 6
</source>
</syntaxhighlight>
{{Works in|[[Dyalog APL]], [[dzaima/APL]], [[NARS2000]]}}
{{Works in|[[Dyalog APL]], [[dzaima/APL]], [[NARS2000]]}}


[[Commute]] can be used to emulate a <source lang=apl inline>f g h</source> [[Fork]] when combined with [[Compose]]:
[[Commute]] can be used to emulate a monadic <syntaxhighlight lang=apl inline>f g h</syntaxhighlight> [[Fork]] when combined with [[Compose]] (note the following code is not workable APL, but a series of evaluations):
<source lang=apl>
<syntaxhighlight lang=apl>
g⍨∘f⍨∘h⍨ x   
g⍨∘f⍨∘h⍨ x   
x g⍨∘f⍨∘h x ⍝ the last ⍨ is being used monadically, i.e. Selfie
x g⍨∘f⍨∘h x ⍝ the last ⍨ is being used monadically, i.e. Selfie
Line 23: Line 23:
(h x) g⍨∘f x ⍝ the last ⍨ is being used dyadically, i.e. Swap
(h x) g⍨∘f x ⍝ the last ⍨ is being used dyadically, i.e. Swap
(h x) g⍨ (f x)  
(h x) g⍨ (f x)  
(f x) g (h x) the last ⍨ is being used dyadically, i.e. Swap
(f x) g (h x) the last ⍨ is being used dyadically, i.e. Swap
</source>
</syntaxhighlight>
 
== History ==
 
The Commute operator was defined with glyph <syntaxhighlight lang=apl inline>⍨</syntaxhighlight> in [[Operators and Functions]] in 1978, and taken up by [[NARS]] as a result. [[Dyalog]], influenced by NARS, included the operator early on. [[SHARP APL]] later added the operator with glyph <syntaxhighlight lang=apl inline>⊂</syntaxhighlight>, and it was included in [[J]] as "Reflex/Passive" (<syntaxhighlight lang=j inline>~</syntaxhighlight>). The [[ISO/IEC 13751:2001]] standard included the operator as defined in NARS, although it was unsupported by mainstream dialects such as [[APLX]] and [[APL+Win]] at the time,<ref>F.H.D. van Batenburg. [http://archive.vector.org.uk/art10000930 Conformity of APL Implementations to the ISO APL Standard]. [[Vector journal]] Volume 21, No.3. 2005-05.</ref> and remains unsupported by [[APL2]].


== External links ==
== External links ==
Line 35: Line 39:


* [https://help.dyalog.com/latest/index.htm#Language/Primitive%20Operators/Commute.htm Dyalog]
* [https://help.dyalog.com/latest/index.htm#Language/Primitive%20Operators/Commute.htm Dyalog]
* [https://mlochbaum.github.io/BQN/doc/swap.html BQN]


== References ==
<references/>
{{APL built-ins}}[[Category:Primitive operators]][[Category:Composition operators]]
{{APL built-ins}}[[Category:Primitive operators]][[Category:Composition operators]]

Latest revision as of 16:56, 16 March 2024

Commute (), or in SHARP APL, is a monadic operator that takes a dyadic function as operand and modifies how the argument(s) of its derived functions are used. If the derived function is used monadically, the argument is also used as left argument. This usage is also known as Self or, more informally, Selfie. If the derived function is used dyadically, the arguments are swapped. This usage is also known as Swap. The glyph is also used for Constant.

Examples

This decrements from the vector:

      1 -⍨ 1 2 3
0 1 2

Double:

      +⍨1 2 3
2 4 6

Commute can be used to emulate a monadic f g h Fork when combined with Compose (note the following code is not workable APL, but a series of evaluations):

g⍨∘f⍨∘h⍨ x   
x g⍨∘f⍨∘h x ⍝ the last ⍨ is being used monadically, i.e. Selfie
x g⍨∘f⍨ h x
(h x) g⍨∘f x ⍝ the last ⍨ is being used dyadically, i.e. Swap
(h x) g⍨ (f x) 
(f x) g (h x) ⍝ the last ⍨ is being used dyadically, i.e. Swap

History

The Commute operator was defined with glyph in Operators and Functions in 1978, and taken up by NARS as a result. Dyalog, influenced by NARS, included the operator early on. SHARP APL later added the operator with glyph , and it was included in J as "Reflex/Passive" (~). The ISO/IEC 13751:2001 standard included the operator as defined in NARS, although it was unsupported by mainstream dialects such as APLX and APL+Win at the time,[1] and remains unsupported by APL2.

External links

Lessons

Documentation

References

  1. F.H.D. van Batenburg. Conformity of APL Implementations to the ISO APL Standard. Vector journal Volume 21, No.3. 2005-05.
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