On Mon, 2020-09-21 at 19:18 +0000, Graydon graydon(_at_)marost(_dot_)ca wrote:
On Mon, Sep 21, 2020 at 07:06:21PM -0000, Dimitre Novatchev
dnovatchev(_at_)gmail(_dot_)com scripsit:
filter($sequence,$function) => head()
Is there any guarantee that the evaluation of filter() will stop as
soon as
an initial item is produced?
I wouldn't think there would be, given that filter() in general is
specified to filter the whole sequence.
In some cases the expression can be rewritten so that filter() is
replaced by $function(head($sequence)); you can only do this if you
know that $function() won't return an empty sequence though, and
$sequence is known at compile time. BaseX does some rewrites in this
vein, although i don't know if it can ever do this one. Oh kinda:
filter( (1 to 1000), function($input as xs:integer) { $input * 2 } )
=> head()
Optimized Query:
head((1 to 1000)[((. mod 2) = 0)])
Similarly, head( (1 to 10000) )
is optimized to
<QueryPlan compiled="true" updating="false">
<Int type="xs:integer" size="1">1</Int>
</QueryPlan>
I've used languages that had infinite (OK, uunbounded) sequences as
first class objects, with lazy evaluation, which can be a very natural
way to think about cases like this. But we don't have them :)
It's always hard to know how much optimizing to do in what is
essentially an interpreter - at somep point you can spend so long
optimizing that you end up slower. But if the optimized queries can be
reused in production it's another matter...mmodulo errors!
--
Liam Quin, https://www.delightfulcomputing.com/
Available for XML/Document/Information Architecture/XSLT/
XSL/XQuery/Web/Text Processing/A11Y training, work & consulting.
Barefoot Web-slave, antique illustrations: http://www.fromoldbooks.org
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--