Array Functions

Arrays are ordered, index-addressable sequences of items, written with square brackets (e.g. [1,2,3]); they were introduced, together with maps, in XPath 3.1 (see XPath). The array: functions below operate on arrays; XPath 4.0, currently under development by the W3C's QT4CG and not yet a Recommendation, is expected to extend array and JSON support further. Each function is evaluated against:
let $a := [1,2,3,"a","b",["c",{"x":0}]]
return …
array:size($a)6
array:get($a,4)a
array:put($a,2,"z")[1, "z", 3, "a", "b", ["c", {"x":0}]]
array:append($a,"z")[1, 2, 3, "a", "b", ["c", {"x":0}], "z"]
array:subarray($a,2,2)[2, 3]
array:remove($a,6)[1, 2, 3, "a", "b"]
array:insert-before($a,2,"z")[1, "z", 2, 3, "a", "b", ["c", {"x":0}]]
array:head($a)1
array:tail($a)[2, 3, "a", "b", ["c", {"x":0}]]
array:reverse($a)[["c", {"x":0}], "b", "a", 3, 2, 1]
array:for-each([2,4,6], function($e){$e*10})[20, 40, 60]
array:filter([2,4,6], function($e){$e<5})[2, 4]
array:fold-left(["a","b","c"], "d", function($acc,$e){$e||$acc})cbad
array:fold-right(["a","b","c"], "d", function($acc,$e){$e||$acc})dcba
array:for-each-pair([1,2,3], ["a","b","c"], function($a,$b){$a||$b})["1a", "2b", "3c"]
array:sort([1,200,30])[1, 30, 200]
array:flatten(([1, 2, 5], [[10, 11], 12], [], 13))1 2 5 10 11 12 13

ch03-array-functions.xpath.txt:
let $a := [1,2,3,"a","b",["c",{"x":0}]]
return array:sort([1,200,30])

[1, 30, 200]