Hook



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 and backhook. BQN uses two operators 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, and soon included in J. This definition specifies that  is   and   is. However, Roger Hui later opined that this definition was better suited to a dyadic operator than an element of syntax, 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 dyadic operator   (" 's left argument comes from  ") and After  to be   (" 's right argument comes from  "). 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.

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        \ ⍵