Skip to content
Sequence Functions

Sequence & Higher-Order Functions

Sequence Operations

Function Description
boolean($arg) Effective boolean value
count($arg) Number of items
data($arg?) Typed values (atomization)
deep-equal($a, $b) Deep structural equality
distinct-values($arg) Remove duplicates
empty($arg) True if empty
exists($arg) True if not empty
exactly-one($arg) Assert exactly one item
one-or-more($arg) Assert at least one item
zero-or-one($arg) Assert at most one item
head($arg) First item
tail($arg) All items except first
index-of($seq, $search) Positions of $search in $seq
insert-before($target, $pos, $inserts) Insert items at position
remove($target, $pos) Remove item at position
reverse($arg) Reverse order
subsequence($source, $start, $length?) Contiguous subsequence
unordered($source) Implementation-dependent order
sort($input, $collation?, $key?) Sort (numeric or string, optional key function)
true(), false(), not($arg) Boolean functions
last(), position() Context position and size

Higher-Order Functions

for-each

for-each($seq, $action) — Apply function to each item.

for-each(1 to 5, function($x) { $x * $x })
(: 1, 4, 9, 16, 25 :)

filter

filter($seq, $predicate) — Keep items where predicate is true.

filter(1 to 10, function($x) { $x mod 2 = 0 })
(: 2, 4, 6, 8, 10 :)

fold-left / fold-right

fold-left($seq, $zero, $f) — Left fold (reduce).

fold-left(1 to 5, 0, function($acc, $x) { $acc + $x })
(: 15 :)

for-each-pair

for-each-pair($seq1, $seq2, $action) — Apply function to pairs.

for-each-pair((1,2,3), (10,20,30), function($a,$b) { $a + $b })
(: 11, 22, 33 :)

apply

apply($function, $array) — Call function with arguments from array.

apply(concat#3, ["a", "b", "c"])    (: "abc" :)

sort with key function

sort(("banana", "apple", "cherry"), (), function($x) { string-length($x) })
(: "apple", "banana", "cherry" :)

Function Introspection

Function Description
function-lookup($name, $arity) Look up function by QName and arity
function-name($func) QName of a named function
function-arity($func) Arity of a function
let $f := function-lookup(xs:QName("fn:abs"), 1)
return $f(-42)                              (: 42 :)

Error and Trace

Function Description
error($code?, $description?, $value?) Raise an error
trace($value, $label?) Returns $value unchanged (debug)