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.
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.
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.
Γ·β2β{6<π©}Β¨ 3βΏ7βΏ2βΏ1βΏ8 β¨ 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 β¨ 2 3 3 β©
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 π©
.
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.
2βΈΓββ¨2,β¨4,Β―2,1β©β© 1 β¨ 4 β¨ 16 0.25 2 β© β©
Regardless of how numbers in π¨πΎπ©
are arranged, π½
is evaluated the minimum number of times required to find the result, and regular (positive) applications are all performed before reverse (negative) ones. So the pattern of application is entirely defined by the smallest and largest values given by πΎ
.