# Composing functions

A tacit function combines other functions without naming its arguments. It often reads as a description of what it computes: `+/÷≢` is the sum divided by the count. When several forms give the same function, write the one that shows its meaning most directly.

The [token separator](syntax.md#token-separator) [`⁝`](https://bpl.fast.ai//glyphs/tricolon.html) separates names and literals without ending their run.

## Trains

A run that ends in a function is a train. A train of two functions is an atop: `f⁝g Y` is `f(g Y)`. A train of three functions is a fork: `f⁝g⁝h Y` is `(f Y)g(h Y)`. With a left argument, `X(f g)Y` is `f(X g Y)` and `X(f g h)Y` is `(X f Y)g(X h Y)`. [Trains](evaluation.md#trains) gives the rules for longer trains.

```bpl
mean←+/÷≢ ◇ mean 2 4 9       ⍝ 5
1 2 (⊣+⊢) 3 4                ⍝ 4 6
```

In a train, a [subject](terms.md#term-subject) becomes the left argument of the function after it. `32+1.8×` converts Celsius to Fahrenheit by multiplying by 1.8, then adding 32.

```bpl
fahrenheit←32+1.8× ◇ fahrenheit 100     ⍝ 212
```

A space keeps a train apart from its argument. `+/÷≢ v` applies the train to `v`. Without the space, the one run `+/÷≢v` reads as `+/÷(≢v)`, the reciprocal of the count `3ₓ`, which [`÷`](glyphs/obelus.md) floors to `0ₓ` for machine integers:

```bpl
v←2 4 9 ◇ +/÷≢ v             ⍝ 5
v←2 4 9 ◇ +/÷≢v              ⍝ 0ₓ
```

## Operators on trains

An operator at the start of a run takes the whole preceding run as its left operand. A space can therefore replace parentheses around a short train: `⊂⊢ ⌸` is `(⊂⊢)⌸`, and `+/÷≢ ¹` is `(+/÷≢)¹`.

```bpl
m←2 3ρι6 ◇ +/÷≢ ¹m        ⍝ 1 4
0 1 0 ⊂⊢ ⌸ "abc"           ⍝ "ac" "b"
```

The operand is the whole preceding run, including a left argument or an earlier function at its start: in `0 1 0⊂⊢ ⌸ "abc"`, the operand is `0 1 0⊂⊢`. Use spaces or parentheses to keep those outside the operand. With two arguments, put a space on each side of the operator, as above. `0 1 0 ⊂⊢ ⌸"abc"` instead evaluates the run `⌸"abc"` first. Named operators work the same way as glyphs. Keep parentheses where they show nested structure more clearly.

## Identity functions

In a train, [`⊢`](https://bpl.fast.ai//glyphs/right-tack.html) stands for the right argument and [`⊣`](https://bpl.fast.ai//glyphs/left-tack.html) for the left. A fork with [`⊢`](https://bpl.fast.ai//glyphs/right-tack.html) reads well when the argument appears once. `⊢÷+/` divides each item by the sum:

```bpl
⊢÷+/ 1 2 5                 ⍝ 0.125 0.25 0.625
2 (⊢-⊣) 5                    ⍝ 3
```

When a formula uses its argument more than once, each [`⊢`](https://bpl.fast.ai//glyphs/right-tack.html) makes the reader track the argument again. A function that removes the repeats reads better. Newton's step towards √2 adds half of `x` to the reciprocal of `x`. `⊽⁻`, the inverse of double, halves:

```bpl
n←⊽⁻+÷ ◇ n 1.5              ⍝ 1.416666667
```

`0.5×⊢+2÷⊢` gives the same results, but its two [`⊢`](https://bpl.fast.ai//glyphs/right-tack.html)s hide the formula. When no clear tacit form exists, write a dfn, as in `{0.5×ω+2÷ω}`.

[Unary](glyphs/superscript-u.md) [`ᵘ`](https://bpl.fast.ai//glyphs/superscript-u.html) calls a function with its right argument alone. Both `fᵘ Y` and `X fᵘ Y` mean `f Y`, as the two-function train `f⊢` does. Use `+/ᵘ⌸` to sum each Key group without using its key.

## Atop and Over

The two-function train `⌊÷` divides, then rounds down:

```bpl
7 ⌊÷ 2                       ⍝ 3ₓ
```

[Atop](glyphs/jot.md) [`∘`](https://bpl.fast.ai//glyphs/jot.html) writes this composition explicitly. `f∘g` applies `g` and then `f`, as the train `f g` does. `X f∘g Y` is `f(X g Y)`.

[Over](glyphs/circle-diaeresis.md) `f⍥g` applies `g` to each argument before `f`. `X f⍥g Y` is `(g X)f(g Y)`. `=⍥≢` tests whether two lists have the same length:

```bpl
"abc" =⍥≢ "xyz"              ⍝ $t
```

## Before and After

[Before](glyphs/right-tail-arrow.md) `A↣f` binds `A` as the left argument of `f`. In a train, a subject binds the same way without the glyph: `2×` is `2↣×`. [After](glyphs/left-tail-arrow.md) `f↢A` binds `A` as the right argument of `f`. `,↢1` appends 1 and `-↢10` subtracts 10:

```bpl
,↢1⁝2 3                     ⍝ 2 3 1
-↢10⁝3                      ⍝ ¯7
```

With two function operands, [`↣`](https://bpl.fast.ai//glyphs/right-tail-arrow.html) and [`↢`](https://bpl.fast.ai//glyphs/left-tail-arrow.html) form monadic [hooks](terms.md#term-hook). `f↣g Y` is `(f Y)g Y` and `f↢g Y` is `Y f(g Y)`. Two-function trains in BPL are atops. `3< ↣# Y` is `(3<Y)#Y`. It keeps the items of `Y` that are greater than 3:

```bpl
3< ↣# 2 7 1 8               ⍝ 7 8
```

## Self and Commute

[Self](glyphs/tilde-diaeresis.md) `f⍨Y` is `Y f Y`. Commute `X f⍨Y` is `Y f X`:

```bpl
,⍨"ab"                       ⍝ "abab"
2-⍨10                        ⍝ 8
```

For a power, a superscript reads more directly than Self: `x²` rather than `×⍨x`.

```bpl
x←3 ◇ x²                     ⍝ 9
```

## Inverse

`f⁻` is the inverse of `f`, as [Superscripts](scripts.md#on-a-function) describes. BPL infers the inverses of most compositions. `fahrenheit⁻` converts back to Celsius:

```bpl
fahrenheit←32+1.8× ◇ fahrenheit⁻ 212   ⍝ 100
```

A superscript straight after a number applies to the number. To invert a function that ends in a number, separate the function from [`⁻`](https://bpl.fast.ai//glyphs/superscript-minus.html) with a space or parentheses:

```bpl
+↢3 ⁻ 10                    ⍝ 7
```

`(+↢3)⁻10` also gives `7`. Without grouping, `+↢3⁻` tries to invert the number `3`, which raises a `DOMAIN` error. The signed superscript `3⁻¹` means one third. On a function, `f⁻¹` means rank ¯1.

## Under

[Under](glyphs/at.md) `f@g` applies `f` to what `g` selects and writes the result back. `10× @[1 3]` multiplies items 1 and 3 by 10. `⌊@(10×)` rounds down to tenths: it scales by 10, rounds down, then scales back.

```bpl
10× @[1 3] 1 2 3 4          ⍝ 1 20 3 40
⌊@(10×)1.25 2.78             ⍝ 1.2 2.7
```

## Separating an operand from its argument

Within a run, an operator's right operand is the one [token](terms.md#term-token) after it. A dyadic operator at the end of a run takes the whole next run as its right operand: in `⌊@ 10× x`, the right operand of [`@`](https://bpl.fast.ai//glyphs/at.html) is `10×`. Literals separated by spaces join into one strand before an operator takes its operand, as [Syntax](syntax.md#runs) describes. `≥⍣3 5` therefore takes `3 5` as its operand and returns the function `≥⍣[3 5]`.

[`⁝`](https://bpl.fast.ai//glyphs/tricolon.html) (Alt-:) separates tokens without ending the run or continuing a literal strand. Use it between an operand and the derived function's right argument where they would otherwise merge. It works inside a longer expression too:

```bpl
≥⍣3⁝5                                       ⍝ 8
m←["r":2 "c":3]ρι6ₓ ◇ i←"c" ◇ +/⍠i⁝m         ⍝ ["r":2]ρ[3 12]ₓ
m←["r":2 "c":3]ρι6ₓ ◇ i←"c" ◇ 10÷√1++/⍠i⁝m  ⍝ ["r":2]ρ[5 2.773500981]
2× ⍣3⁝5                                    ⍝ 40
+/÷≢ ⍤1⁝2 3ρι6                              ⍝ 1 4
```

Add no separator where the tokens already stay distinct. A name never joins a literal strand, as in `f⍣n1`. A vector argument written in brackets can touch its operand, as in `≥⍣2[1 2 3]`.

[`⁝`](https://bpl.fast.ai//glyphs/tricolon.html) works the same way with a left argument. With a left argument, you can instead put parentheses around the derived function. Use whichever reads better. Compound operands keep their own parentheses:

```bpl
w←[1 2◇3 4◇5 6] ◇ 10 20+⍤1⁝w                 ⍝ [11 22◇13 24◇15 26]
w←[1 2◇3 4◇5 6] ◇ 10 20(+⍤1)w                ⍝ [11 22◇13 24◇15 26]
x←1.25 2.78 ◇ ⌊@(10×)x                       ⍝ 1.2 2.7
```

## Forms that read worse

Each pair gives the same result with one argument.

| Reads worse | Reads better |
|--- |--- |
| `{(+/ω)÷≢ω}` | `+/÷≢` |
| `0.5×⊢+2÷⊢` | `⊽⁻+÷` |
| `{ω÷+/ω}` | `÷↢ +/` |
| `{ω×10}@[1 3]` | `10× @[1 3]` |
| `{+/ω}⍠0` | `+/₀` |
| `{ω,1}` | `,↢1` |
| `×⍨x` | `x²` |
| `≥⍣3⊢5` | `≥⍣3⁝5` |
| `+/⍠i⊢m` | `+/⍠i⁝m` |

A dfn still reads better when its body needs local names or predicates, or when a tacit form would be longer or clearly awkward. [Style](style.md) covers grouping and spacing.
