Split composition: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
mNo edit summary
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''Split composition''' is a [[tacit]] pattern, used to pre-process argument(s) with the outer-most operands before applying the middle operand between the result. Given functions <syntaxhighlight lang=apl inline>f</syntaxhighlight>, <syntaxhighlight lang=apl inline>g</syntaxhighlight>, and <syntaxhighlight lang=apl inline>h</syntaxhighlight>, the split composition on arguments <syntaxhighlight lang=apl inline>x</syntaxhighlight> and <syntaxhighlight lang=apl inline>y</syntaxhighlight> is defined as <syntaxhighlight lang=apl inline>(f x) g (h y)</syntaxhighlight>.
{{Glyphbox|f⍛g∘h}}[[File:Split_composition.png|frameless|right|200px]]
'''Split composition''' is a [[tacit]] pattern, used to pre-process argument(s) with the outer-most operands before applying the middle operand between the result. Given functions <syntaxhighlight lang=apl inline>f</syntaxhighlight>, <syntaxhighlight lang=apl inline>g</syntaxhighlight>, and <syntaxhighlight lang=apl inline>h</syntaxhighlight>, a split composition on arguments <syntaxhighlight lang=apl inline>x</syntaxhighlight> and <syntaxhighlight lang=apl inline>y</syntaxhighlight> is defined as <syntaxhighlight lang=apl inline>(f x) g (h y)</syntaxhighlight>.


This construct was introduced by the [[I|I language]] as "split-compose", where it is represented with <syntaxhighlight lang=apl inline>O</syntaxhighlight>. <syntaxhighlight lang=apl inline>O</syntaxhighlight> also represents the [[Over]] operator, as it is a split composition with identical outer operands.  
This construct was introduced by the [[I|I language]] as "split-compose", where it is represented with <syntaxhighlight lang=apl inline>O</syntaxhighlight> which also represents the [[Over]] operator - as it too is a split composition with identical outer operands.  


This doesn't appear as a primitive in any APL, nor can it, because it [[composition|composes]] three functions, while a [[Function composition|compositional operator]] can take no more than two [[operands]]. This situation is identical to that of the [[fork]]. Both split compositions and forks can be constructed using companion operators, tying together the three involved functions.
This doesn't appear as a primitive in any APL, nor can it, because it [[composition|composes]] three functions, while a [[Function composition|compositional operator]] can take no more than two [[operands]]. This situation is identical to that of the [[fork]]. Both split compositions and forks can be constructed using companion operators, tying together the three involved functions.
Line 18: Line 19:
== Alternatives ==
== Alternatives ==


In dialects that lack Reverse Compose (and even Compose), split compositions can be denoted either by defining the missing operator(s), or as a single derived function or [[fork]], if this is supported. For example, in [[Dyalog APL]] the expression can be formed with Compose and [[Commute]] (<syntaxhighlight lang=apl inline>⍨</syntaxhighlight>) as <syntaxhighlight lang=apl inline>g⍨∘f⍨∘h</syntaxhighlight>:
In dialects that lack Reverse Compose (and even Compose), split compositions can be denoted either by defining the missing operator(s), or as a single derived function or [[fork]], if this is supported. For example, in [[Dyalog APL]] the pattern can be formed with Compose and [[Commute]] (<syntaxhighlight lang=apl inline>⍨</syntaxhighlight>) as <syntaxhighlight lang=apl inline>g⍨∘f⍨∘h</syntaxhighlight>:
<syntaxhighlight lang=apl>
<syntaxhighlight lang=apl>
       5 ×⍨∘⍳⍨∘| 5 ¯8 ¯2 ¯5 3
       5 ×⍨∘⍳⍨∘| 5 ¯8 ¯2 ¯5 3

Latest revision as of 08:45, 23 September 2022

f⍛g∘h
Split composition.png

Split composition is a tacit pattern, used to pre-process argument(s) with the outer-most operands before applying the middle operand between the result. Given functions f, g, and h, a split composition on arguments x and y is defined as (f x) g (h y).

This construct was introduced by the I language as "split-compose", where it is represented with O which also represents the Over operator - as it too is a split composition with identical outer operands.

This doesn't appear as a primitive in any APL, nor can it, because it composes three functions, while a compositional operator can take no more than two operands. This situation is identical to that of the fork. Both split compositions and forks can be constructed using companion operators, tying together the three involved functions.

In Extended Dyalog APL and dzaima/APL, a split composition can be formed using Reverse Compose () and Compose (). In this example, we multiply the interval (integers up until) of the left argument, with the Magnitude of the right:

      5 ⍳⍛×∘| 5 ¯8 ¯2 ¯5 3
5 16 6 20 15

This is evaluated as (⍳5) × (|5 ¯8 ¯2 ¯5 3). A further example concatenates the reciprocal of the left argument with the negation of the right:

      2 ÷⍛,- 4
0.5 ¯4

This is evaluated as (÷2) × (-4).

Alternatives

In dialects that lack Reverse Compose (and even Compose), split compositions can be denoted either by defining the missing operator(s), or as a single derived function or fork, if this is supported. For example, in Dyalog APL the pattern can be formed with Compose and Commute () as g⍨∘f⍨∘h:

      5 ×⍨∘⍳⍨∘| 5 ¯8 ¯2 ¯5 3
5 16 6 20 15
      2(,⍨∘÷⍨∘-)4
0.5 ¯4

Note that g∘h⍨∘f⍨ applies f before h which can matter for functions with side effects. For example, consider the following where 'x' f⍛g∘h 'y' would print hfg:

      f←{⍞←⊃⎕SI}
      g←{⍞←⊃⎕SI}
      h←{⍞←⊃⎕SI}
      'x' g⍨∘f⍨∘h 'y'
hfg
      'x' g∘h⍨∘f⍨ 'y'
fhg

The equivalent fork is f⍤⊣ g h⍤⊢, for example:

      5 (⍳⍤⊣×|⍤⊢) 5 ¯8 ¯2 ¯5 3
5 16 6 20 15
      2(÷⍤⊣,-⍤⊢)4
0.5 ¯4
APL syntax [edit]
General Comparison with traditional mathematicsPrecedenceTacit programming (Train, Hook, Split composition)
Array Numeric literalStringStrand notationObject literalArray notation (design considerations)
Function ArgumentFunction valenceDerived functionDerived operatorNiladic functionMonadic functionDyadic functionAmbivalent functionDefined function (traditional)DfnFunction train
Operator OperandOperator valenceTradopDopDerived operator
Assignment MultipleIndexedSelectiveModified
Other Function axisBracket indexingBranchStatement separatorQuad nameSystem commandUser commandKeywordDot notationFunction-operator overloadingControl structureComment