In BQN, it's strongly preferred to use functions, and not modifiers, for array manipulation. Functions are simpler as they have fewer moving parts. They are more concrete, since the array results can always be viewed right away. They are easier to implement with reasonable performance as well, since there is no need to recognize many possible function operands as special cases.

The Window function replaces APL's Windowed Reduction, J's more general Infix operator, and Dyalog's Stencil, which is adapted from one case of J's Cut operator.

We'll start with the one-axis case. Here Window's left argument is a number between `0`

and `1+β π©`

. The result is composed of slices of `π©`

(contiguous sections of major cells) with length `π¨`

, starting at each possible index in order.

5β"abcdefg" ββ β΅"abcde bcdef cdefg" β

There are `1+(β π©)-π¨`

, or `(β π©)Β¬π¨`

, of these sections, because the starting index must be at least `0`

and at most `(β π©)-π¨`

. Another way to find this result is to look at the number of cells in or before a given slice: there are always `π¨`

in the slice and there are only `β π©`

in total, so the number of slices is the range spanned by these two endpoints.

You can take a slice of an array `π©`

that has length `l`

and starts at index `i`

using Take with Drop or Rotate: `lβiβπ©`

or `lβiβ½π©`

. The Prefixes function returns all the slices that end at the end of the array (`(β π©)=i+l`

), and Suffixes gives the slices that start at the beginning (`i=0`

). Windows gives yet another collection of slices: the ones that have a fixed length `l=π¨`

. Selecting one cell from its result gives you the slice starting at that cell's index:

2β5β"abcdefg" "cdefg" 5β2β"abcdefg" "cdefg"

Windows differs from Prefixes and Suffixes in that it doesn't add a layer of nesting (it doesn't enclose each slice). This is possible because the slices have a fixed size.

The above description applies to a higher-rank right argument. As an example, we'll look at two-row slices of a shape `3βΏ4`

array. For convenience, we will enclose each slice. Note that slices always have the same rank as the argument array.

<β2 2β"0123"βΎ"abcd"β"ABCD" ββ Β· ββ ββ β΅"0123 β΅"abcd abcd" ABCD" β β β

Passing a list as the left argument to Windows takes slices along any number of leading axes. Here are all the shape `2βΏ2`

slices:

<β2 2βΏ2β"0123"βΎ"abcd"β"ABCD" ββ β΅ ββ ββ ββ β΅"01 β΅"12 β΅"23 ab" bc" cd" β β β ββ ββ ββ β΅"ab β΅"bc β΅"cd AB" BC" CD" β β β β

The slices are naturally arranged along multiple dimensions according to their starting index. Once again the equivalence `iβlβx`

ββ `lβiβx`

holds, provided `i`

and `l`

have the same length.

If `π¨`

has length `0`

, then `π©`

is not sliced along any dimensions. The only slice that resultsβthe entire argumentβis then arranged along an additional zero dimensions. In the end, the result is `π©`

, unchanged.

`π©`

is an array. `π¨`

is a number, or numeric list or unit, with `π¨β€ββ β’π©`

. The result `z`

has shape `π¨βΎΒ¬βπ¨βΎ((β π¨)βΈβ)β’π©`

, and element `iβz`

is `π©βΛ(β π¨)(β+βΎ((β π¨)βΈβ)β)i`

.

Using Group we could also write `iβz`

ββ `π©βΛ(π¨βΎβ(βββ )β’π©) +Β΄Β¨ββ i`

.

Let's look at an earlier example, along with its Transpose (`β`

).

{β¨π©,βπ©β©}5β"abcdefg" ββ Β· ββ ββ β΅"abcde β΅"abc bcdef bcd cdefg" cde β def efg" β β

Although the two arrays have different shapes, they are identical where they overlap.

βοΈβ‘β(3βΏ3βΈβ)ββ5β"abcdefg" 1

In other words, the i'th element of slice j is the same as the j'th element of slice i: it is the `i+j`

'th element of the argument. So transposing still gives a possible result of Windows, but with a different slice length.

{(5βπ©)β‘β(3βπ©)}"abcdefg" 1

In general, we need a more complicated transposeβswapping the first set of `β π¨`

axes with the second set. Note again the use of Span, our slice-length to slice-number converter.

{((5βΏ6Β¬2βΏ2)βπ©) β‘ 2βΏ3β(2βΏ2βπ©)} β5βΏ6βΏ7 1

Windows can be followed up with a reduction on each slice to give a windowed reduction. Here we take running sums of 3 values.

βοΈ+ΛΛ3β β¨2,6,0,1,4,3β© β¨ 8 7 5 8 β©

A common task is to act on windows with an initial or final element so the total length stays the same. When using windows of length 2, the best way to accomplish this is with a shift `Β«`

or `Β»`

. If the window length is longer or variable, then a trick with Windows works better: add the elements, and then use windows matching the original length. Here we invert Plus Scan `+``

, which requires we take pairwise differences starting at initial value 0.

-β(0Β»β’) +` 3βΏ2βΏ1βΏ1 β¨ 3 2 1 1 β© (-ΛΛβ β0βΎβ’) +` 3βΏ2βΏ1βΏ1 β¨ 3 2 1 1 β©

With Windows, we can modify the 3-element running sum from before to keep the length constant by starting with two zeros.

βοΈ(+Λβ β(2β₯0)βΈβΎ) β¨2,6,0,1,4,3β© β¨ 2 8 8 7 5 8 β©