saxon:generate-sequence
Returns a sequence based on a start item and a function that computes the next item.
generate-sequence($start as item(), $step as fn(item(), xs:integer) as item()?) ➔ item()*
Arguments | ||||
| $start | item() | The first item | |
| $step | fn(item(), xs:integer) as item()? | Function to compute the next item | |
Result | item()* | |||
Namespace
http://saxon.sf.net/
Saxon availability
Requires Saxon-PE or Saxon-EE. Implemented since Saxon 13. Available for all platforms.
Notes on the Saxon implementation
Available since Saxon 13. Originally proposed as a new standard QT4 function (see PR2350) but not (yet) accepted for inclusion in the spec.
Details
This function returns a sequence obtained by successive evaluations of the $step
function. Each call on $step gets two arguments, (a) the current item in the sequence,
and (b) the 1-based position in the sequence. The first item in the sequence is $start.
Using this function requires care because it can return an infinite sequence. It is evaluated lazily.
Evaluation terminates either when the $step function returns the empty sequence, or when
no further items from the result sequence are required.
For example, the call:
saxon:generate-sequence(20, fn{.+1}) [1 to 10]returns the first 10 numbers in the infinite sequence (10, 11, 12, ...).
Similarly,
(saxon:generate-sequence([1,1], fn{[?2, ?1+?2]}) ?1) [ 1 to $N ]generates the first $N numbers in the Fibonacci sequence (1, 1, 2, 3, 5, 8, 13, ...).
These examples rely on lazy evaluation, and on the fact that Saxon recognizes the predicate
[1 to $N] and will not evaluate more items than are needed to satisfy this predicate.