Commute: Difference between revisions

Jump to navigation Jump to search
1,345 bytes added ,  22:06, 10 September 2022
m
Text replacement - "<source" to "<syntaxhighlight"
(Add a bad example...)
m (Text replacement - "<source" to "<syntaxhighlight")
(15 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Built-in|Commute|⍨}} is a [[Primitive function|primitive]] [[monadic]] [[operator]] that takes a [[function]] on its left and returns a new function with swapped arguments.
{{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 [[Constant]].


== Examples ==
== Examples ==


Subtract one from every item of the input array.
This decrements from the vector:
<source lang=apl>
<syntaxhighlight lang=apl>
1 -⍨ 1 2 3
      1 -⍨ 1 2 3
</source>
0 1 2
</syntaxhighlight>
{{Works in|[[Dyalog APL]], [[dzaima/APL]], [[NARS2000]]}}
Double:
<syntaxhighlight lang=apl>
      +⍨1 2 3
2 4 6
</syntaxhighlight>
{{Works in|[[Dyalog APL]], [[dzaima/APL]], [[NARS2000]]}}
 
[[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):
<syntaxhighlight lang=apl>
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
</syntaxhighlight>
 
== History ==
 
The Commute operator was defined in [[Operators and Functions]] in 1978, and taken up by [[NARS]] as a result. [[Dyalog]], influenced by NARS, included the operator early on. While it didn't appear in [[SHARP APL]], it was included in [[J]] as "Reflex/Passive" (<syntaxhighlight lang=j inline>~</syntaxhighlight>).


== External links ==
== External links ==
Line 12: Line 34:
=== Lessons ===
=== Lessons ===


* [https://chat.stackexchange.com/transcript/52405?m=40900200#40900200 APL Cultivation]
* [https://chat.stackexchange.com/rooms/52405/conversation/lesson-3-some-apl-operators-----#40900200 APL Cultivation]


=== Documentation ===
=== Documentation ===


* [http://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]
* [http://microapl.com/apl_help/ch_020_020_400.htm APLX]
* [https://mlochbaum.github.io/BQN/doc/swap.html BQN]
* J [https://www.jsoftware.com/help/dictionary/d121.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/minusdot NuVoc]


{{APL built-ins}}
{{APL built-ins}}[[Category:Primitive operators]][[Category:Composition operators]]

Navigation menu