Hook: Difference between revisions

Jump to navigation Jump to search
72 bytes added ,  21:17, 10 September 2022
m
Text replacement - "<source" to "<syntaxhighlight"
(→‎External links: BQN docs instead of tutorial)
m (Text replacement - "<source" to "<syntaxhighlight")
Line 3: Line 3:
A '''hook''' is an asymmetrical form of function composition that first applies one of the composed functions to one argument, then applies the other function to one argument and the result. 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 (<code>h</code>) and backhook (<code>H</code>). [[BQN]] uses two [[operator]]s Before (<code>⊸</code>) and After (<code>⟜</code>), which also serve the purpose of the [[Bind]] operator.
A '''hook''' is an asymmetrical form of function composition that first applies one of the composed functions to one argument, then applies the other function to one argument and the result. 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 (<code>h</code>) and backhook (<code>H</code>). [[BQN]] uses two [[operator]]s Before (<code>⊸</code>) and After (<code>⟜</code>), which also serve the purpose of the [[Bind]] operator.


The hook as an operator first appeared in [[A Dictionary of APL]] as [[Withe]]. 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 <source lang=j inline>(F G) y</source> is <source lang=j inline>y F G y</source> and <source lang=j inline>x (F G) y</source> is <source lang=j inline>x F G y</source>. However, [[Roger Hui]] later opined that this definition was better suited to a dyadic operator (which could be denoted <source lang=j inline>h.</source>) 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 to 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 <source lang=j inline>x F h. G y</source>, that is, the dyadic case. The monadic functionality can be achieved using [[Commute]] as <source lang=apl inline>F∘G⍨y</source> and the full [[ambivalent]] function can be written as <source lang=apl inline>F∘G⍨⍨</source>.
The hook as an operator first appeared in [[A Dictionary of APL]] as [[Withe]]. 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</source> is <syntaxhighlight lang=j inline>y F G y</source> and <syntaxhighlight lang=j inline>x (F G) y</source> is <syntaxhighlight lang=j inline>x F G y</source>. However, [[Roger Hui]] later opined that this definition was better suited to a dyadic operator (which could be denoted <syntaxhighlight lang=j inline>h.</source>) 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 to 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</source>, that is, the dyadic case. The monadic functionality can be achieved using [[Commute]] as <syntaxhighlight lang=apl inline>F∘G⍨y</source> and the full [[ambivalent]] function can be written as <syntaxhighlight lang=apl inline>F∘G⍨⍨</source>.


In [[I]] and [[BQN]], there are two hooks in order to maintain symmetry: for example, BQN defines Before (<code>⊸</code>) to be the dyadic operator <code>{(𝔽𝕨⊣𝕩)𝔾𝕩}</code> ("<code>𝔾</code>'s left argument comes from <code>𝔽</code>") and After (<code>⟜</code>) to be <code>{(𝕨⊣𝕩)𝔽𝔾𝕩}</code> ("<code>𝔽</code>'s right argument comes from <code>𝔾</code>"). In the dyadic case these functions are identical to [[Reverse Compose]] and [[Beside]] respectively, but in the monadic case they differ 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.
In [[I]] and [[BQN]], there are two hooks in order to maintain symmetry: for example, BQN defines Before (<code>⊸</code>) to be the dyadic operator <code>{(𝔽𝕨⊣𝕩)𝔾𝕩}</code> ("<code>𝔾</code>'s left argument comes from <code>𝔽</code>") and After (<code>⟜</code>) to be <code>{(𝕨⊣𝕩)𝔽𝔾𝕩}</code> ("<code>𝔽</code>'s right argument comes from <code>𝔾</code>"). In the dyadic case these functions are identical to [[Reverse Compose]] and [[Beside]] respectively, but in the monadic case they differ 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.

Navigation menu