Array.reverse
func Array.reverse[T](xs: Array[T]): Array[T]Array.reverse returns a new array holding the elements of xs in reverse order.
Details
- Returns a new array;
xsis unchanged. - The new array holds the same elements, not copies, so a nested array, map, set, or record is shared with
xs. - To reverse text, reverse its graphemes, not its code points.
Basic examples
Reverse an array of numbers:
Array.reverse([1, 2, 3]) // [3, 2, 1]The original array keeps its order:
let xs = ["a", "b", "c"]
Array.reverse(xs) // ["c", "b", "a"]
xs // ["a", "b", "c"]Scope
An empty array and an array of one element come back with the same elements:
let none: Array[int] = []
none.reverse() // []
["a"].reverse() // ["a"]Records, null, and nested arrays move as whole elements:
type Point = { x: int, y: int }
[Point{ x: 1, y: 2 }, Point{ x: 3, y: 4 }].reverse() // [Point{ x: 3, y: 4 }, Point{ x: 1, y: 2 }]
[1, null, 3].reverse() // [3, null, 1]
[[1, 2], [3]].reverse() // [[3], [1, 2]]Call it through . on an array, or with a pipe:
[1, 2, 3].reverse() // [3, 2, 1]
[1, 2, 3] | Array.reverse() // [3, 2, 1]Name the argument:
Array.reverse(xs: [1, 2]) // [2, 1]Applications
Reverse a string by its graphemes, and join them back:
let reversed = "noe\u{301}l" | String.graphemes() | Array.reverse() | String.join("")
reversed == "le\u{301}on" // trueCheck whether a word is a palindrome:
func isPalindrome(word: string): bool {
let gs = word.toLower().graphemes()
gs == gs.reverse()
}
isPalindrome("Level") // true
isPalindrome("Ant") // falseShow the latest entries of a log first:
let log = ["start", "load config", "listen on :8080"]
for line in log.reverse() {
println(line)
}listen on :8080
load config
startRead a path from the leaf up to the root:
let parts = "usr/local/bin".split("/")
String.join(parts.reverse(), " < ") // "bin < local < usr"Properties and relations
Reversing twice gives back the original array:
let xs = [1, 2, 3, 4]
assert(xs.reverse().reverse() == xs)The length stays the same, and element i of the result is element length - 1 - i of xs:
let xs = [5, 6, 7, 8]
let ys = xs.reverse()
assert(len(ys) == len(xs))
for i in Array.range(0, len(xs)) {
assert(ys[i] == xs[len(xs) - 1 - i])
}The result is a new array, even when the order cannot change:
let xs = [1]
let ys = xs.reverse()
assert(ys == xs)
assert(!Array.same(ys, xs))The first element of the result is the last element of xs:
let xs = [1, 2, 3]
assert(xs.reverse().first() == xs.last())Reversing a concatenation reverses each part and swaps them:
let xs = [1, 2]
let ys = [3, 4, 5]
assert(Array.concat(xs, ys).reverse() == Array.concat(ys.reverse(), xs.reverse()))Possible issues
Array.reverse does not change its argument. A call whose result is dropped does nothing:
var xs = [1, 2, 3]
xs.reverse()
println(xs)
xs = xs.reverse()
println(xs)[1, 2, 3]
[3, 2, 1]The result shares its elements with xs. Writing into an element that is an array or a record shows through the original:
let grid = [[1], [2]]
let flipped = grid.reverse()
flipped[0][0] = 9
grid // [[1], [9]]Array.reverse takes an array, not a string; split the string with String.graphemes first, as below:
Array.reverse("noel") // error: cannot use string where Array[T] is expectedReversing the code points of a string moves an accent onto another letter. Split with String.graphemes instead of String.chars, as in Applications above:
String.join("noe\u{301}l".chars().reverse(), "") == "l\u{301}eon" // trueReversing a stable sort also reverses the order of equal keys. To sort in descending order and keep equal elements in their original order, sort by a negated key or with Array.sortWith:
type Player = { name: string, score: int }
let players = [Player{ name: "Ann", score: 2 }, Player{ name: "Bob", score: 1 }, Player{ name: "Cid", score: 2 }]
players.sortBy({ x.score }).reverse().map({ x.name }) // ["Cid", "Ann", "Bob"]
players.sortBy({ -x.score }).map({ x.name }) // ["Ann", "Cid", "Bob"]The sorts treat -0.0 and 0.0 as equal and keep them in their original order, so reversing a sort swaps them too. Dividing by each zero shows which is which:
let zs = [-0.0, 0.0]
zs.sort().map({ 1.0 / x }) // [-Infinity, Infinity]
zs.sort().reverse().map({ 1.0 / x }) // [Infinity, -Infinity]See also
Array.sort, Array.sortBy, Array.sortWith, Array.slice, Array.concat, Array.same, String.graphemes, String.chars
