# Reverse and Rotate

The symbol `β½` indicates two different array transformations: with no left argument, it reverses the major cells of the array, but with a left argument, it rotates or cycles them around. These two possibilities, first put together in very early versions of APL, can't be considered restrictions or different views of some unifying function, but there are connections between them. Each returns an array with the same shape and all the same elements as `π©`, possibly in a different arrangement. And elements that start out next to each other in `π©` generally stay next to each otherβalways, if we consider an element on one edge to be next to the one opposite to it. One might think of them as isometries preserving a discrete subgroup of the torus, if one were inclined to think such things. On major cells, the two functions decompose the dihedral group okay I'll stop.

If there's no reason the data should be seen as cyclic or periodic, it's best to avoid Rotate: shift functions are probably more appropriate.

## Reverse

Reverse doesn't make things complicated. It puts the elements of a list the other way around, or more generally the major cells of an array.

βοΈ
```    β½ "abcdefg"
"gfedcba"

β½ >"ab"βΏ"cd"βΏ"ef"
ββ
β΅"ef
cd
ab"
β

β½ 'c'
Error: β½: Argument cannot be a unit
```

You can't reverse an atom or rank-0 array because it has no axes to reverse along, or it could be said no ordering to reverse.

To reverse along an axis other than the first, use Cells (`Λ`) or Rank (`β`).

βοΈ
```    β½Λ >"ab"βΏ"cd"βΏ"ef"
ββ
β΅"ba
dc
fe"
β
```

Reverse is useful for folding left to right instead of right to left (here we use Pair to show structure).

βοΈ
```    β Β΄   "abcd"  # Right to left

βΛΒ΄ β½ "abcd"  # Left to right
```

Reverse is its own inverse `β½βΌ`. So with Under, `π½βΎβ½` reverses the argument, applies `π½`, and reverses again. It's a particularly useful pattern with Scan, as it allows scanning from the end rather than the beginning of the array. For example, `β¨`` on a list of booleans changes all bits after the first `1` to `1`, but `β¨`βΎβ½` does this to all bits before the last `1`.

βοΈ
```    β¨`   0βΏ0βΏ1βΏ0βΏ0βΏ1βΏ0
β¨ 0 0 1 1 1 1 1 β©

β¨`βΎβ½ 0βΏ0βΏ1βΏ0βΏ0βΏ1βΏ0
β¨ 1 1 1 1 1 1 0 β©
```

## Rotate

Rotate moves elements in a list around cyclically. It can also rotate any number of axes of the argument array by different amounts at once. That's discussed in the next section; for now we'll stick to a single number for `π¨`. It has to be an integer, and `π©` has to be an array with at least one axis.

βοΈ
```    2 β½ "rotate"
"tatero"

2 (β’ β β½) 5βΏ2β₯"rotateCELL"
ββ
Β· ββ     ββ
β΅"ro   β΅"te
ta     CE
te     LL
CE     ro
LL"    ta"
β      β
β

2 β½ 'c'  # No axes to rotate
Error: β½: π© must have rank at least 1 for atom π¨
```

Elements are always rotated to the left, so that entry `i` of the result is entry `π¨+i` of the argumentβor rather, entry `(β π©)|π¨+i` to enable elements to cycle around. This can be seen directly by using the range `βn` as an argument: then the value of `π©` at index `i` is just `i`.

βοΈ
```    2 β½ β6
β¨ 2 3 4 5 0 1 β©
```

The rotation `(β π©)β½π©` moves each element the entire length of `π©`, which just places it back where it started. In fact, adding `β π©` to the rotation amount never changes the behavior or the rotation. In terms of indices, this is because `(β π©)|(β π©)+a` is `a`.

To rotate the other way, use a negative left argument (so `-βΈβ½` is a simple way to write "reverse rotate"). This will always be the same as some leftwards rotation, since `(-r)β½π©` is `((β π©)-r)β½π©`, but could be more convenient.

βοΈ
```    Β―2 β½ "rotate"
"terota"
```

### Multiple axes

The easiest way to rotate along a later array axis is usually to use the Cells (`Λ`) or Rank (`β`) modifier.

βοΈ
```    β’ tab β 3βΏ4β₯"abcdABCD0123"
ββ
β΅"abcd
ABCD
0123"
β

1 β½Λ tab  # Rotate the second axis
ββ
β΅"bcda
BCDA
1230"
β
```

Rotate also allows `π¨` to be a list (or unit array) of integers, in which case they're matched with leading axes of `π©`. This means the length of `π¨` can't be larger than the rank of `π©`, or there wouldn't be enough axes to match. This rule that `π©` has to have rank one or more when `π¨` is an atom is a special case, because then `π¨` is treated as the one-element list `β₯π¨`.

βοΈ
```    3βΏ4βΏ2 β½ "just a list"
```

The expression below rotates the first (vertical) axis of `tab` by one element, and second by two. So the line of capital letters goes from being one away from the top, up to the top, and the column with `'2'` goes from horizontal index 2 to index 0.

βοΈ
```    1βΏ2 β½ tab
ββ
β΅"CDAB
2301
cdab"
β
```

The vertical and horizontal rotations are independent, and could also be done with two `β½`s and a `Λ`. The multi-axis form is more convenient, and can potentially be evaluated faster than multiple separate rotations in the cases where it shows up.

βοΈ
```    1 β½ 2 β½Λ tab
ββ
β΅"CDAB
2301
cdab"
β
```