# Repeat

Repeat (β) is a 2-modifier that applies its operand function π½ multiple times.

βοΈ
Β»Β»Β» "ABCDE"
"   AB"

Β»β3 "ABCDE"
"   AB"

In mathematics (which unsurpisingly tends to use complicated terms to talk about an easy concept), this kind of repetition is called an iterated function and written with exponential notation. It's related to function composition β in the same way that exponentiation (β) relates to multiplication (Γ): function iteration is repeated composition.

nβ4  ββ  nΓnΓnΓn
Fβ4  ββ  FβFβFβF

Fβ0 repeats F zero times, that is, does nothing. Like nβ0 gives the multiplicative identity 1, Fβ0 is the compositional identity, β’. Since Fβ1 applies F and Fβ0 doesn't, Repeat might be pronounced "if" or "conditional" when πΎ is boolean.

BQN's Repeat modifier has some extra functionality relative to the mathematical version. It allows a left argument, and some extensions to the right operand πΎ. As usual for 2-modifiers, πΎ is actually a function that applies to the arguments to give a result. The result can be a natural number as shown above, or a negative number to Undo (βΌ) π½, or an array of values.

## Left argument

If π¨ is given, it's passed as the left argument to π½ for every invocation.

βοΈ
3 +β2 7
13
3 + 3 + 7
13

This kind of composition can't be represented by β anymore (you'd need a train), but it's not much of a leap. π¨ π½βn π© is always equivalent to π¨βΈπ½βn π©, provided n is a constantβnot a function, as discussed in the next section.

## Dynamic repetition count

In the general case, πΎ is a function, which is applied to all arguments to get the repetition count. That is, the actual count is π¨πΎπ©.

βοΈ
βΎβ1ββ’ 4
β¨ 4 1 1 1 1 β©

1βΈ+ββ  β4
β¨ 4 5 6 7 β©

The most common use is the case where πΎ is a condition that returns 0 or 1. Then Repeat simply applies π½ if the condition holds. For example, the following code halves numbers that are greater than 6.

βοΈ
β¨ 3 3.5 2 1 4 β©

If π¨ is given, then πΎ gets it as a left argument (to avoid this, use π¨βΈπ½βπΎ π©, which applies πΎ to π© only). This form also works well with a boolean condition.

βοΈ
3 β£β<Β¨ 2βΏ4βΏ6  # Left if less, i.e. minimum

## Negative repetition

What does it mean to repeat a function a negative number of times? For a negative integer -n, BQN defines Fβ(-n) to be FβΌβn. In particular, FβΒ―1 simply undoes F.

βοΈ
1 β½βΒ―1 "abcde"  # Rotate backwards
"eabcd"

Because BQN's Undo is a little looser than a strict mathematical inverse, this is an extension of the function inverse written fβ»ΒΉ in mathematics. As a result, it doesn't have all the same properties. For natural numbers, Repeat follows the rule that Fβm Fβn π© is Fβ(m+n) π©. With a negative, we have π© β‘ Fβn Fβ(-n) π©, but not necessarily π© β‘ Fβ(-n) Fβn π©.

## Array of repetition counts

The value of π¨πΎπ© might also be an array, whose elements are any valid repetition valuesβintegers, or other arrays. Each integer in the nested structure is replaced with the result of repeating π½ that many times.

βοΈ