Hook

From APL Wiki
Jump to navigation Jump to search
Hook operators in BQN

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 (h) and backhook (H). 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[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 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 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 ⟩
Works in: BQN

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

  1. ↑ Ken Iverson and Eugene McDonnell. Phrasal forms at APL89.
  2. ↑ Roger Hui. Hook Conjunction?. J Wiki essays. 2006. Accessed 2021-02-08.
APL built-ins [edit]
Primitive functions
Scalar
Monadic Conjugate βˆ™ Negate βˆ™ Signum βˆ™ Reciprocal βˆ™ Magnitude βˆ™ Exponential βˆ™ Natural Logarithm βˆ™ Floor βˆ™ Ceiling βˆ™ Factorial βˆ™ Not βˆ™ Pi Times βˆ™ Roll βˆ™ Type βˆ™ Imaginary βˆ™ Square Root
Dyadic Add βˆ™ Subtract βˆ™ Times βˆ™ Divide βˆ™ Residue βˆ™ Power βˆ™ Logarithm βˆ™ Minimum βˆ™ Maximum βˆ™ Binomial βˆ™ Comparison functions βˆ™ Boolean functions (And, Or, Nand, Nor) βˆ™ GCD βˆ™ LCM βˆ™ Circular βˆ™ Complex βˆ™ Root
Non-Scalar
Structural Shape βˆ™ Reshape βˆ™ Tally βˆ™ Depth βˆ™ Ravel βˆ™ Enlist βˆ™ Table βˆ™ Catenate βˆ™ Reverse βˆ™ Rotate βˆ™ Transpose βˆ™ Raze βˆ™ Mix βˆ™ Split βˆ™ Enclose βˆ™ Nest βˆ™ Cut (K) βˆ™ Pair βˆ™ Link βˆ™ Partitioned Enclose βˆ™ Partition
Selection First βˆ™ Pick βˆ™ Take βˆ™ Drop βˆ™ Unique βˆ™ Identity βˆ™ Select βˆ™ Replicate βˆ™ Expand βˆ™ Set functions (Intersection βˆ™ Union βˆ™ Without) βˆ™ Bracket indexing βˆ™ Index
Selector Index generator βˆ™ Grade βˆ™ Index Of βˆ™ Interval Index βˆ™ Indices βˆ™ Deal
Computational Match βˆ™ Not Match βˆ™ Membership βˆ™ Find βˆ™ Nub Sieve βˆ™ Encode βˆ™ Decode βˆ™ Matrix Inverse βˆ™ Matrix Divide βˆ™ Format βˆ™ Execute βˆ™ Materialise βˆ™ Range
Primitive operators Monadic Each βˆ™ Commute βˆ™ Constant βˆ™ Replicate βˆ™ Expand βˆ™ Reduce βˆ™ Windowed Reduce βˆ™ Scan βˆ™ Outer Product βˆ™ Key βˆ™ I-beam βˆ™ Spawn βˆ™ Function axis
Dyadic Bind βˆ™ Compositions (Compose, Reverse Compose, Beside, Atop, Over) βˆ™ Inner Product βˆ™ Power βˆ™ At βˆ™ Under βˆ™ Rank βˆ™ Depth βˆ™ Variant βˆ™ Stencil βˆ™ Cut (J)
Quad names
Arrays Index origin βˆ™ Migration level βˆ™ Atomic vector
Functions Name class βˆ™ Case convert βˆ™ Unicode convert
Operators Search βˆ™ Replace