Ken Tune wrote:

  Hi,

> Folks more knowledgable than me, correct me if I’m wrong but
> $seq[n] is shorthand for $seq[posn() = n]

  When "n" evaluates to a singleton integer, "$s[n]" is equivalent to
"$s[position() eq n]".

> which is why something like
> (1 to 10)[xdmp:random(9) + 1] can yield counter-inituitive results.

  I don't understand that (except that xdmp:random can be counter-
intuitive indeeed).

> The above would imply that short-circuiting as described is
> therefore not consistent with required behavior.

  When you have a singleton integer predicate (a positional predicate)
you can always evaluate the sequence from its left-hand expression,
and stop as soon as you found a match.  You don't need to evaluate the
rest of the expression.

  In fact, if you have 2 functions f and g, returning exactly ONE
integer each (no more, no less), I think the optimizer is allowed to
rewrite "(f(), g())[2]" as "g()".

  Unless you want to preserve side-effects.  But then you don't want
to short0cut anyway.  That's why new annotations have been introduced
in XQuery 3.0 to allow the user to specify whether a function has
side-effects or not.

> How about fn:head((1,xdmp:sleep(1))) to solve this though?

  fn:head is defined formally as equivalent to "[1]".  The difference
is that, being a function, you can create a function item out of it,
and pass it to or return it from a higher-order function (and combined
with fn:tail, it helps the optimizer to detect head-tail recursion).

> fn:head new at 6.0

  Yes, fn:head is new in F&O 3.0.

  Regards,

-- 
Florent Georges
http://fgeorges.org/
http://h2oconsulting.be/
_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to