# 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.

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.

Name `Cmp` `Cmp π©` `π¨ Cmp π©` Unified Train
Before `FβΈG` `(Fπ©) G π©` `(Fπ¨) G π©` `{(π½π¨β£π©)πΎπ©}` `Fββ£ G β’`
After `Fβ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

9 βββ 2
```

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
```

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
```

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
```

## 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:

`Cmp` `0βΈ<` `<β0`
` Cmp x` `0 < x` `x < 0`
`w Cmp x` `0 < x` `w < 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 explicit 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 `βΎββ¨ββ©βΈβ₯`.

## Combinations

If you like to go tacit, you'll likely end up stringing together a few `βΈ`s and `β`s at times. Of course the effects are entirely determined by the left-to-right precedence rule for modifiers, but it's interesting to examine what happens in more detail.

In the pattern `FβΈGβH`, the ordering doesn't matter at all! That is, it means `(FβΈG)βH`, but this is the same function as `FβΈ(GβH)`. In both cases, `F` is applied to `π¨`, `H` is applied to `π©`, and `G` acts on both the results (the parentheses do change whether `F` or `H` is called first, which only matters if they have side effects).

βοΈ
```    4 -βΈβββ 2
```

I once named this pattern "split compose", but now I think it makes more sense to think of it as two pre-functions added separately to one central function (`β` above). The whole is exactly the sum of its parts. When applied to just one argument, `π©` is reused on both sides, making the composition equivalent to a 3-train.

βοΈ
```    -βΈβββ 2

(-ββ) 2  # Same thing
```

More `β`s can be added on the right, making `π©` flow through all the added functions. So for example `FβGβH x` is `x F G H x`, and could also be written `Fβ(G H) x`.

A sequence of `βΈ`s is more interesting. It doesn't just compose the functions (for that you need `GβFβΈH`, but note the weird orderingβ`F` applies before `G`!), but instead passes the current value and the initial function each time. Consider `FβΈGβΈHβΈI`, or `((FβΈG)βΈH)βΈI`: every function but `F` is on the ring side, meaning it's dyadic!

Here's a long example, that might show up if you want to sort an array but have an intolerance for the character `β§`. In quicksort, you select a partition element from the array, then divide it into elements less than, and greater than or equal to, the pivot. You'd probably pick a random element for the pivot, but here I'll go with the middle element to avoid having a webpage that generates differently every time!

βοΈ
```    (ββ Γ·2Λ)       "quicksort"  # Index of the pivot
4

(ββ Γ·2Λ)βΈβ     "quicksort"  # Select pivot from π©
'k'

(ββ Γ·2Λ)βΈββΈβ€   "quicksort"  # Compare with π©
β¨ 1 1 0 0 1 1 1 1 1 β©

(ββ Γ·2Λ)βΈββΈβ€βΈβ "quicksort"  # Use to partition π©
Three is rare, but I use two `βΈ`s all the time, as well as `β` followed by `βΈ`, for example the `<β'a'βΈ/` filter on the front page. I think a combination like `lotsβofβstuffβΈ/ x` reads very nicely when moving from right to left. When I see `βΈ/` I know that I'm filtering `x` and can read the rest with that context. The reason `βΈ` has all this power, but not `β`, has nothing to do with the modifiers themselves, as they're completely symmetrical. It's all in the way BQN defines modifier grammar, left to right.