The shift functions `«`

and `»`

add major cells to one side an array, displacing cells on the opposite side and moving those in between. Shifts resemble but are more general than the bit-based shift operations used in low-level languages. They replace the APL pattern of a 2-wise reduction after appending or prepending an element (APL's `2≠/0,v`

translates to `»⊸≠v`

); a nice version of this common pattern is one reason BQN is free to replace windowed reduction with the sometimes less convenient Windows.

The result of a shift function always has the same shape as `𝕩`

. The function adds major cells to the beginning (`»`

) or end (`«`

) of `𝕩`

, moving the cells already in `𝕩`

to accomodate them. Some cells on the opposite side from those added will "fall off" and not be included in the result.

0‿0 » 3‿2‿1 # Shift Before ⟨ 0 0 3 ⟩ "end" « "add to the " # Shift After " to the end"

The cells to add come from `𝕨`

if it's present, as shown above. Otherwise, a single cell of fill elements for `𝕩`

is used. This kind of shift, which moves cells in `𝕩`

over by just one, is called a "nudge".

» "abcd" # Nudge " abc" « 1‿2‿3 # Nudge Back ⟨ 2 3 0 ⟩

If `𝕨`

is longer than `𝕩`

, some cells from `𝕨`

will be discarded, plus all of `𝕩`

. In this case `𝕨»𝕩`

is `(≠𝕩)↑𝕨`

and `𝕨«𝕩`

is `(-≠𝕩)↑𝕨`

. For similar reasons, nudging an array of length 0 returns it unchanged.

When working with a sequence of data such as text, daily measurements, or audio data, shift functions are generally the best way to handle the concept of "next" or "previous". In the following example `s`

is shown alongside the shifted-right data `»s`

, and each element is compared to the previous with `-⟜»`

, which we see is the inverse of Plus Scan `+``

.

s ← 1‿2‿2‿4‿3‿5‿6 s ≍ »s ┌─ ╵ 1 2 2 4 3 5 6 0 1 2 2 4 3 5 ┘ -⟜» s ⟨ 1 1 0 2 ¯1 2 1 ⟩ +` -⟜» s # Same as s ⟨ 1 2 2 4 3 5 6 ⟩

In this way `»`

refers to a sequence containing the previous element at each position. By default the array's fill is used for the element before the first, and a right argument can be given to provide a different one.

∞ » s ⟨ ∞ 1 2 2 4 3 5 ⟩ ⊏⊸» s ⟨ 1 1 2 2 4 3 5 ⟩

It may appear backwards that `»`

, which typically means "go to the next item", is used to represent the previous item. In fact there is no conflict: the symbol `»`

describes what position each cell of `𝕩`

will have in the result, but in this context we are interested in knowing what argument value occurs in a particular result position. By moving all numbers into the future we ensure that a number in the present comes from the past. To keep your intuition functioning in these situations, it may help to think of the arrow point as fixed at some position in the result while the tail stretches back to land on the argument position where it comes from.

Switching the direction of the arrow, we get an operation that pulls the next value into each position:

↗️s ≍ «s ┌─ ╵ 1 2 2 4 3 5 6 2 2 4 3 5 6 0 ┘ «⊸- s ⟨ 1 0 2 ¯1 2 1 ¯6 ⟩

The differences here are the same as `-⟜» s`

, except that they are shifted over by one, and it is the *last* value in the sequence that is compared with a fill value, not the first. These techniques adapt easily to more complicated operations. A symmetric difference is found by subtracting the previous element from the next, and dividing by two:

2÷˜ (»-«) s ⟨ ¯1 ¯0.5 ¯1 ¯0.5 ¯0.5 ¯1.5 2.5 ⟩ 2÷˜ (⊣˝⊸» - ⊢˝⊸«) s # Repeat at the ends instead of using fills ⟨ ¯0.5 ¯0.5 ¯1 ¯0.5 ¯0.5 ¯1.5 ¯0.5 ⟩

A feature these examples all share is that they maintain the length of `s`

. This is a common condition in sequence processing, particularly when the processed sequence needs to be combined or compared with the original in some way. However, it's not always the case. In some instances, for example when searching `s`

to see if there is any value less than the previous, the list should get shorter during processing. In these cases, Windows (`↕`

) is usually a better choice.

The glyphs `«`

and `»`

, suggesting movement, were chosen for the same reasons as the digraphs `<<`

and `>>`

in C-like languages, and can be used to implement the same bit-shift operations on boolean lists.

⊢ i ← "10011011"-'0' ⟨ 1 0 0 1 1 0 1 1 ⟩ «⍟3 i # Quick and dirty left shift ⟨ 1 1 0 1 1 0 0 0 ⟩ 3 ⥊⟜0⊸« i # Alternate left shift ⟨ 1 1 0 1 1 0 0 0 ⟩

With a number in big-endian format, a right shift might be logical, shifting in zeros, or arithmetic, shifting in copies of the highest-order bit (for little-endian numbers, this applies to left shifts rather than right ones). The two kinds of shift can be performed with similar code, using `0`

or `⊏𝕩`

for the inserted cell.

3 ⥊⟜0⊸» i # Logical right shift ⟨ 0 0 0 1 0 0 1 1 ⟩ 3 (⥊⟜⊏»⊢) i # Arithmetic right shift ⟨ 1 1 1 1 0 0 1 1 ⟩

In Take (`↑`

), there's no way to specify the fill element when the result is longer than the argument. To take along the first axis with a specified, constant fill value, you can use Shift Before instead, where the right argument is an array of fills with the desired final shape (a more general approach is Under).

"abc" » 5⥊'F' "abcFF"

When using Scan (```

), the result at a particular index is obtained from values up to and including the one at that index. But it's common to want to use the values up to but not including that one instead. This can be done either by joining or shifting in that value before scanning. The difference is that with Join the result is longer than the argument. Either form might be wanted depending on how it will be used.

2 +` 1‿0‿1‿0 # Initial value not retained ⟨ 3 3 4 4 ⟩ 2 +`∘∾ 1‿0‿1‿0 # All values ⟨ 2 3 3 4 4 ⟩ 2 +`∘» 1‿0‿1‿0 # Final value not created ⟨ 2 3 3 4 ⟩

The *strides* of an array are the distances between one element and the next along any given axis. It's the product of all axis lengths below that axis, since these are all the axes that have to be "skipped" to jump along the axis. The strides of an array `𝕩`

are `(×`1»⊢)⌾⌽ ≢𝕩`

.

(×`1»⊢)⌾⌽ 5‿2‿4‿3 ⟨ 24 12 3 1 ⟩

Shifting always works on the first axis of `𝕩`

(which must have rank 1 or more), and shifts in major cells. A left argument can have rank equal to `𝕩`

, or one less than it, in which case it becomes a single cell of the result. With no left argument, a cell of fills `1↑0↑𝕩`

is nudged in.

⊢ a ← ⥊⟜(↕×´) 4‿3 ┌─ ╵ 0 1 2 3 4 5 6 7 8 9 10 11 ┘ » a # Nudge adds a cell of fills ┌─ ╵ 0 0 0 0 1 2 3 4 5 6 7 8 ┘ "one" « a # Shift in a cell ┌─ ╵ 3 4 5 6 7 8 9 10 11 'o' 'n' 'e' ┘ ("two"≍"cel") « a # Shift in multiple cells ┌─ ╵ 6 7 8 9 10 11 't' 'w' 'o' 'c' 'e' 'l' ┘

In any instance of `»`

or `«`

, `𝕩`

must have rank at least 1.

For a dyadic shift function, `𝕨`

must be Join-compatible with `𝕩`

(that is, `𝕨∾𝕩`

completes without error) and can't have greater rank than `𝕩`

. Then Shift Before (`»`

) is `{(≠𝕩)↑𝕨∾𝕩}`

and Shift After (`«`

) is `{(-≠𝕩)↑𝕩∾𝕨}`

When called monadically, the default argument is a cell of fills `1↑0↑𝕩`

. That is, Nudge (`»`

) is `(1↑0↑⊢)⊸»`

and Nudge Back (`«`

) is `(1↑0↑⊢)⊸«`

. This default argument always satisfies the compatibility requirement above and so the only conditions for nudge are that `𝕩`

has rank at least 1 and has a fill element.