Hook: Difference between revisions
(Created page with "thumb|right|Hook operators in [[BQN]] A '''hook''' is an asymmetrical form of function composition that first applies one of the composed functions to one...") |
(No difference)
|
Revision as of 19:12, 8 February 2021
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 hook (h
) and backhook (H
). BQN uses two modifiers Before (βΈ
) and After (β
), which also serve the purpose of the Bind operator.
The meaning of 2-train as hook was first proposed in Iverson and McDonnell's paper Phrasal Forms introducing trains[1], and 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 conjunction (dyadic operator) than an element of syntax,[2] and defined to 2-train to represent Atop instead when he led the introduction of trains to Dyalog APL.
In I and BQN, there are two hooks in order to maintain symmetry: for example, BQN defines Before (βΈ
) to be the 2-modifier {(π½π¨β£π©)πΎπ©}
("πΎ
's left argument comes from π½
") and After (β
) to be {(π¨β£π©)π½πΎπ©}
("π½
's right argument comes from πΎ
"). In the two-argument case these functions are identical to Reverse Compose and Beside respectively, but in the one-argument 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.
Like Reverse Compose, 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
References
- β Ken Iverson and Eugene McDonnell. Phrasal forms at APL89.
- β Roger Hui. Hook Conjunction?. J Wiki essays. 2006. Accessed 2021-02-08.