Split composition: Difference between revisions

Jump to navigation Jump to search
1,369 bytes added ,  08:45, 23 September 2022
m
no edit summary
mNo edit summary
mNo edit summary
 
(10 intermediate revisions by 3 users not shown)
Line 1: Line 1:
'''Split-compose''' is a point-free construct, used to pre-process its argument(s) with the left and right-most operand before applying the middle operand between the result. The name was introduced by the [[I|I language]], where it's represented with <code>O</code>, a higher-order function that applies first to the middle function and then the two outer functions (<code>O</code> also represents the [[Over]] operator). It doesn't appear as a primitive in any APL.
{{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>.


Given functions <source lang=apl inline>F</source>, <source lang=apl inline>G</source>, and <source lang=apl inline>H</source>, the split composition on arguments <source lang=apl inline>x</source> and <source lang=apl inline>y</source> is defined as <source lang=apl inline>(F x) G (H y)</source>.
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.  


In [[BQN]], the construct can be formed using [[Before]] (<code>⊸</code>) and [[After]] (<code>⟜</code>), two [[Function composition|compositional]] [[operator]]s. In this example, we multiply the range of the left argument, with the absolute value of the right.
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.
<source>
    5 ↕⊸×⟜| 5‿¯8‿¯2‿¯5‿3
⟨ 0 8 4 15 12 ⟩
</source>


[[Monadic]]ally, the right argument can be [[selfie|duplicated]] and split-compose will be applied asymmetrically, similar to [[hook]]
In [[Extended Dyalog APL]] and [[dzaima/APL]], a split composition can be formed using [[Reverse Compose]] (<code></code>) and [[Compose]] (<code></code>). In this example, we multiply the [[interval]] (integers up until) of the left argument, with the [[Magnitude]] of the right:
<source>
<syntaxhighlight lang=apl>
    +´⊸÷⟜≠ ↕10
      5 ⍳⍛×∘| 5 ¯8 ¯2 ¯5 3
⟨ 4.5 ⟩
5 16 6 20 15
# This is evaluated as (↕10) +´⊸÷⟜≢ ↕10
</syntaxhighlight>
</source>
This is evaluated as <syntaxhighlight lang=apl inline>(⍳5) × (|5 ¯8 ¯2 ¯5 3)</syntaxhighlight>. A further example concatenates the reciprocal of the left argument with the negation of the right:
 
<syntaxhighlight lang=apl>
In this case, split-compose is precisely equivalent to a [[fork]].
      2 ÷⍛,- 4
 
0.5 ¯4
<source>
</syntaxhighlight>
    (+´÷≠) ↕10
This is evaluated as <syntaxhighlight lang=apl inline>(÷2) × (-4)</syntaxhighlight>.
4.5
== Alternatives ==
</source>
 
 
 
In dialects that lack Before, split-compose can be defined differently.
 
In [[Dyalog APL]] you may form the expression with [[Beside]] <code>()</code> and [[Commute]] <code>(⍨)</code>
 
<source lang=apl>
g∘h⍨∘f⍨
 
g⍨∘f⍨∘h⍨⍨
</source>
 
For example:
<source lang=apl>
    ÷⍨∘(+/)⍨∘≢⍨⍨ ⍳10
4.5
</source>
 
Whilst these expressions will return the same results (if the functions f, g, and h are pure) the evaluation order is not identical.
 
And (most commonly) as a fork:
 
<source lang=apl>
f⍤⊣ g h⍤⊢
</source>
 
== See also ==
 
* [[Tacit programming]]


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>
      5 ×⍨∘⍳⍨∘| 5 ¯8 ¯2 ¯5 3
5 16 6 20 15
      2(,⍨∘÷⍨∘-)4
0.5 ¯4
</syntaxhighlight>
Note that <syntaxhighlight lang=apl inline>g∘h⍨∘f⍨</syntaxhighlight> applies <syntaxhighlight lang=apl inline>f</syntaxhighlight> before <syntaxhighlight lang=apl inline>h</syntaxhighlight> which can matter for functions with side effects. For example, consider the following where <syntaxhighlight lang=apl inline>'x' f⍛g∘h 'y'</syntaxhighlight> would print <code>hfg</code>:
<syntaxhighlight lang=apl>
      f←{⍞←⊃⎕SI}
      g←{⍞←⊃⎕SI}
      h←{⍞←⊃⎕SI}
      'x' g⍨∘f⍨∘h 'y'
hfg
      'x' g∘h⍨∘f⍨ 'y'
fhg
</syntaxhighlight>
The equivalent fork is <syntaxhighlight lang=apl inline>f⍤⊣ g h⍤⊢</syntaxhighlight>, for example:
<syntaxhighlight lang=apl>
      5 (⍳⍤⊣×|⍤⊢) 5 ¯8 ¯2 ¯5 3
5 16 6 20 15
      2(÷⍤⊣,-⍤⊢)4
0.5 ¯4
</syntaxhighlight>
{{APL syntax}}[[Category:Primitive operators]]
{{APL syntax}}[[Category:Primitive operators]]
26

edits

Navigation menu