|
declare variable $n as xs:integer external := 1000;
|
|
|
|
let $put := function($map as map(*), $key as xs:anyAtomicType, $value as item()*) as map(*)
|
|
{ Q{http://www.w3.org/2005/xpath-functions/map}put($map, $key, $value) },
|
|
$gen1ToInf :=
|
|
map{
|
|
"initialized": true(),
|
|
"endReached": false(),
|
|
"getCurrent": function($this as map(*)) { $this?last + 1},
|
|
"moveNext": function($this as map(*))
|
|
{
|
|
if(not($this?initialized)) then $put($this, "initialized", true())
|
|
else $put($this, "last", $this?last +1)
|
|
},
|
|
"last" : 0
|
|
},
|
|
$emptyGenerator := function()
|
|
{
|
|
map{
|
|
"initialized" : true(),
|
|
"endReached": true(),
|
|
"getCurrent": function($this as map(*)) {error()},
|
|
"moveNext": function($this as map(*)) {error()}
|
|
}
|
|
},
|
|
|
|
$skip-helper := function($gen as map(*), $n as xs:integer, $self as function(*))
|
|
{
|
|
if($n eq 0) then $gen
|
|
else
|
|
(
|
|
let $gen := if(not($gen?initialized)) then $gen?moveNext()
|
|
else $gen
|
|
return
|
|
if(not($gen?endReached)) then $self($gen?moveNext($gen), $n -1, $self)
|
|
else $gen
|
|
)
|
|
},
|
|
$skip := function($gen as map(*), $n as xs:integer)
|
|
{
|
|
$skip-helper($gen, $n, $skip-helper)
|
|
},
|
|
|
|
$take-helper := function($gen as map(*), $n as xs:integer, $self as function(*))
|
|
{
|
|
let $gen := if(not($gen?initialized)) then $gen?moveNext()
|
|
else $gen
|
|
return
|
|
if( $gen?endReached or $n eq 0) then $emptyGenerator()
|
|
else $put($gen, "moveNext", $self($gen?moveNext($gen), $n -1, $self))
|
|
},
|
|
|
|
$take := function($gen as map(*), $n as xs:integer)
|
|
{
|
|
$take-helper($gen, $n, $take-helper)
|
|
},
|
|
|
|
$subrange := function($gen as map(*), $m as xs:integer, $n as xs:integer) as map(*)
|
|
{
|
|
$take($skip($gen, $m -1), $n)
|
|
},
|
|
|
|
$at := function($gen as map(*), $ind as xs:integer)
|
|
{
|
|
let $genResult :=$subrange($gen, $ind, 1)
|
|
return
|
|
$genResult?getCurrent($genResult)
|
|
}
|
|
return
|
|
$at($gen1ToInf, $n)
|