Hook: Difference between revisions
(→External links: BQN docs instead of tutorial) |
m (Text replacement - "first-class function" to "first-class function") |
||
(3 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
{{Built-ins|Hooks|⍛|⍩}} are asymmetrical forms of [[function composition]] that first apply one of the composed [[function]]s to one [[argument]], then applies the other function to one argument and the result. [[Kap]] has the two [[operator]]s [[Beside|Compose]] (<syntaxhighlight lang=apl inline>∘</syntaxhighlight>) and [[Inverse compose]] (<syntaxhighlight lang=apl inline>⍛</syntaxhighlight>). [[BQN]] has Before (<code>⊸</code>) and After (<code>⟜</code>), which also serve the purpose of the [[Bind]] operator. In [[J]], a 2-[[train]] is a hook, while [[I]] adds the mirror image to give two functions (I has [[first-class function]]s but no operators) hook (<code>h</code>) and backhook (<code>H</code>). | |||
Hook as an operator first appeared in [[A Dictionary of APL]] as [[Withe]] (<syntaxhighlight lang=apl inline>⍩</syntaxhighlight>). However, [[Ken Iverson|Iverson]] and [[Eugene McDonnell|McDonnell]]'s paper ''Phrasal Forms'', which introduced [[train]]s<ref>[[Ken Iverson]] and [[Eugene McDonnell]]. [http://www.jsoftware.com/papers/fork.htm Phrasal forms] at [[APL89]].</ref>, proposed hook as the meaning of a 2-train, and this was soon included in [[J]]. This definition specifies that <syntaxhighlight lang=j inline>(F G) y</syntaxhighlight> is <syntaxhighlight lang=j inline>y F G y</syntaxhighlight> and <syntaxhighlight lang=j inline>x (F G) y</syntaxhighlight> is <syntaxhighlight lang=j inline>x F G y</syntaxhighlight>. However, [[Roger Hui]] later opined that this definition was better suited to a dyadic operator (which could be denoted <syntaxhighlight lang=j inline>h.</syntaxhighlight>) than an element of syntax,<ref>[[Roger Hui]]. [https://code.jsoftware.com/wiki/Essays/Hook_Conjunction%3F Hook Conjunction?]. J Wiki essays. 2006. Accessed 2021-02-08.</ref> and defined the 2-train to represent [[Atop]] instead when he led the introduction of trains to [[Dyalog APL]]. By that time, Dyalog had long had the [[Beside]] (originally called ''Compose'') operator which is equivalent to <syntaxhighlight lang=j inline>x F h. G y</syntaxhighlight>, that is, the dyadic case. The monadic functionality can be achieved using [[Commute]] as <syntaxhighlight lang=apl inline>F∘G⍨y</syntaxhighlight> and the full [[ambivalent]] function can be written as <syntaxhighlight lang=apl inline>F∘G⍨⍨</syntaxhighlight>. | |||
[[File:Hooks.png|thumb|right|Hook operators in [[BQN]]]] | [[File:Hooks.png|thumb|right|Hook operators in [[BQN]]]] | ||
In order to maintain symmetry, [[Kap]] changes the meaning of <syntaxhighlight lang=apl inline>∘</syntaxhighlight> to be a proper hook, and thus aligns itself with [[I]] and [[BQN]], which also have two hooks. Kap defines Compose (<syntaxhighlight lang=apl inline>∘</syntaxhighlight>) to be the dyadic operator <syntaxhighlight lang=apl inline>{⍺←⍵ ⋄ ⍺ ⍺⍺ ⍵⍵ ⍵}</syntaxhighlight> and Inverse Compose (<syntaxhighlight lang=apl inline>⍛</syntaxhighlight>) to be <syntaxhighlight lang=apl inline>{⍺←⍵ ⋄ (⍺⍺ ⍺) ⍵⍵ ⍵}</syntaxhighlight>. In the dyadic case these operators are identical to [[Beside]] and [[Reverse Compose]] respectively, but in the monadic case, <syntaxhighlight lang=apl inline>∘</syntaxhighlight> differs from Beside because the argument is used twice: the second function application takes it as an argument directly in addition to the result of the first function application. | |||
The two hooks can be used together to form a [[split-compose]] construct: | |||
<pre> | <pre> | ||
3 ¯1 4 ×⍛×∘| ¯2 ¯7 1 | |||
2 ¯7 1 | |||
</pre>{{Works in|[[ | </pre>{{Works in|[[Kap]]}} | ||
This definition behaves differently that the Compose-based one when only one argument is given | This definition behaves differently that the Compose-based one when only one argument is given; in that case, it becomes a monadic 3-[[train]]. | ||
The name "hook" was chosen based on the hook shape of a function call diagram such as the one below, taken from ''Phrasal Forms''. | The name "hook" was chosen based on the hook shape of a function call diagram such as the one below, taken from ''Phrasal Forms''. |
Latest revision as of 02:09, 6 September 2024
⍛ ⍩
|
Hooks (⍛
, ⍩
) are asymmetrical forms of function composition that first apply one of the composed functions to one argument, then applies the other function to one argument and the result. Kap has the two operators Compose (∘
) and Inverse compose (⍛
). BQN has Before (⊸
) and After (⟜
), which also serve the purpose of the Bind operator. In J, a 2-train is a hook, while I adds the mirror image to give two functions (I has first-class functions but no operators) hook (h
) and backhook (H
).
Hook as an operator first appeared in A Dictionary of APL as Withe (⍩
). However, Iverson and McDonnell's paper Phrasal Forms, which introduced trains[1], proposed hook as the meaning of a 2-train, and this was soon included in J. This definition specifies that (F G) y
is y F G y
and x (F G) y
is x F G y
. However, Roger Hui later opined that this definition was better suited to a dyadic operator (which could be denoted h.
) than an element of syntax,[2] and defined the 2-train to represent Atop instead when he led the introduction of trains to Dyalog APL. By that time, Dyalog had long had the Beside (originally called Compose) operator which is equivalent to x F h. G y
, that is, the dyadic case. The monadic functionality can be achieved using Commute as F∘G⍨y
and the full ambivalent function can be written as F∘G⍨⍨
.
In order to maintain symmetry, Kap changes the meaning of ∘
to be a proper hook, and thus aligns itself with I and BQN, which also have two hooks. Kap defines Compose (∘
) to be the dyadic operator {⍺←⍵ ⋄ ⍺ ⍺⍺ ⍵⍵ ⍵}
and Inverse Compose (⍛
) to be {⍺←⍵ ⋄ (⍺⍺ ⍺) ⍵⍵ ⍵}
. In the dyadic case these operators are identical to Beside and Reverse Compose respectively, but in the monadic case, ∘
differs from Beside because the argument is used twice: the second function application takes it as an argument directly in addition to the result of the first function application.
The two hooks can be used together to form a split-compose construct:
3 ¯1 4 ×⍛×∘| ¯2 ¯7 1 2 ¯7 1
This definition behaves differently that the Compose-based one when only one argument is given; in that case, it becomes a monadic 3-train.
The name "hook" was chosen based on the hook shape of a function call diagram such as the one below, taken from Phrasal Forms.
⍺(fg)⍵ ←→ ⍺fg⍵ f / \ ⍺ g \ ⍵
See also
External links
- J Dictionary, NuVoc
- BQN
References
- ↑ Ken Iverson and Eugene McDonnell. Phrasal forms at APL89.
- ↑ Roger Hui. Hook Conjunction?. J Wiki essays. 2006. Accessed 2021-02-08.