From 8ad841e92f70fba73a5c778aa972186387f9ff5c Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Fri, 15 Apr 2022 19:23:10 -0400 Subject: Documentation for Before and After --- docs/doc/hook.html | 161 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 docs/doc/hook.html (limited to 'docs/doc/hook.html') diff --git a/docs/doc/hook.html b/docs/doc/hook.html new file mode 100644 index 00000000..22054800 --- /dev/null +++ b/docs/doc/hook.html @@ -0,0 +1,161 @@ + + + + BQN: Before and After + + +

Before and After

+

(This joke has already been claimed by APL, unfortunately)

+

Also see this tutorial section for an introduction that doesn't require so much context to understand.

+ + + + Before + + π”½βŠΈπ”Ύ 𝕩 + + + + + + + 𝔾 + 𝔽 + 𝕩 + + + 𝕨 π”½βŠΈπ”Ύ 𝕩 + + + + + + + + 𝔾 + 𝔽 + 𝕨 + 𝕩 + + + + + After + + π”½βŸœπ”Ύ 𝕩 + + + + + + + 𝔽 + 𝔾 + 𝕩 + + + 𝕨 π”½βŸœπ”Ύ 𝕩 + + + + + + + + 𝔽 + 𝔾 + 𝕨 + 𝕩 + + + + +

The "hook" combinators Before and After serve a few purposes in BQN. The important thing to remember: the pointy side goes towards the first function to be executed, and the next function that returns the final result is at the ring side. If the pointy-side function is actually a constant like a number, then the ring-side function just gets applied to that constant and one of the arguments. This is the thing Haskell programmers are constantly telling each other isn't called currying, or "Bind" in BQN.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameCmpCmp 𝕩𝕨 Cmp 𝕩UnifiedTrain
BeforeF⊸G(F𝕩) G 𝕩(F𝕨) G 𝕩{(π”½π•¨βŠ£π•©)𝔾𝕩}F∘⊣ G ⊒
AfterF⟜G𝕩 F (G𝕩)𝕨 F (G𝕩){(π•¨βŠ£π•©)𝔽𝔾𝕩}⊣ F G∘⊒
+

Description

+

In the general case, I think of Before as using 𝔽 as a preprocessing function applied to 𝕨 (when there are two arguments) and After as using 𝔾 as preprocessing for 𝕩. Then the other operand is called on the result and remaining argument. Here are some simple calls with Pair (β‹ˆ): the result is a pair that corresponds to 𝕨‿𝕩, but one or the other result has been modified by the pointy-side function.

+↗️
    9 βˆšβŠΈβ‹ˆ 2
+⟨ 3 2 ⟩
+
+    9 β‹ˆβŸœβ†• 2
+⟨ 9 ⟨ 0 1 ⟩ ⟩
+
+

When only one argument is given, it's used in both positions, so that the arguments to the final function are 𝕩 and a function applied to 𝕩.

+↗️
    β‹ˆβŸœβ†• 5
+⟨ 5 ⟨ 0 1 2 3 4 ⟩ ⟩
+
+

This can be used to make a "filter" pattern using Replicate (/). The difference is that Replicate takes a list 𝕩 and boolean list 𝕨 indicating which elements to keep, but filter should take a list and a function that says whether to keep each element. The pattern is F¨⊸/ x, expanding to (FΒ¨x) / x. Here's a list filtered with the function {𝕩<0}.

+↗️
    {𝕩<0}¨⊸/ 4β€ΏΒ―2β€Ώ1β€ΏΒ―3β€ΏΒ―3
+⟨ ¯2 ¯3 ¯3 ⟩
+
+

As < is a pervasive function, there's no need for the Each (Β¨) in this case, and the clunky block function {𝕩<0} can also be written smaller with a combinator, as <⟜0. More on that in the next section…

+↗️
    <⟜0⊸/ 4β€ΏΒ―2β€Ώ1β€ΏΒ―3β€ΏΒ―3
+⟨ ¯2 ¯3 ¯3 ⟩
+
+

Bind

+

"Bind" isn't a special case of Before and After, but instead a description of one way to use them. Let's take a look at the example from the previous section:

+↗️
    <⟜0  4β€ΏΒ―2β€Ώ1β€ΏΒ―3β€ΏΒ―3
+⟨ 0 1 0 1 1 ⟩
+
+

If we expand <⟜0 x, we get x < (0 x), which doesn't quite make sense. That's because 0 has a subject role, but ⟜ always applies its operands as functions. It's more accurate to use x < (0{𝔽} x), or just skip ahead to x < 0.

+

Similar reasoning gives the following expansions:

+ + + + + + + + + + + + + + + + + + + + +
Cmp0⊸<<⟜0
Cmp x0 < xx < 0
w Cmp x0 < xw < 0
+

Note that when there are two arguments, the constant "swallows" the one on the same side, so that the function is applied to the constant and the argument on the opposite side.

+

As in a train, if you want to use a function as a constant then you need to be explicity about it, with the Constant (Λ™) modifier.

+↗️
    3 β‹ˆβŸœ(βŒŠΛ™)⊸β₯Š 'a'+↕12
+β”Œβ”€      
+β•΅"abcd  
+  efgh  
+  ijkl" 
+       β”˜
+
+

In the more extreme case of wanting a modifier operand, you might try β‹ˆβŸœ({∘}Λ™)⊸β₯Š, or (βŠ£β‹ˆ{∘}Λ™)⊸β₯Š, or just cheat with ∾⟜⟨∘⟩⊸β₯Š.

-- cgit v1.2.3