This task, in fact, raises a different question about generator functions.
Consider a hypothetic function ex:fibonacci($Fn-1, $Fn-2) that returns an
infinitive sequence of fibonacci numbers.
To use it, one just accesses its result sequence by index.
<xsl:function name="ex:fibonachi" as="xs:integer*">
<xsl:param value="Fn-1" as="xs:integer"/>
<xsl:param value="Fn-2" as="xs:integer"/>
<xsl:variable name="Fn" as="xs:integer" select="$Fn-1 + $Fn-2"/>
<xsl:sequence select="$Fn"/>
<xsl:sequence select="ex:fibonachi($Fn, $Fn-1)"/>
</xsl:function>
Honestly, I'm not entirely sure if it's a legal technique in xslt, but
definitely it's not supported in saxon,
and it would be good if it were supported, as it allows separation of
iterator and iteration logic.
See
http://www.nesterovsky-bros.com/weblog/2010/04/09/GeneratorFunctionsInXslt.a
spx
--
Vladimir Nesterovsky
http://www.nesterovsky-bros.com/
Are there any XSLT processors that convert tree recursive
functions into more efficient iterative procedures?
/Roger
-------------------------------------------------
Version #1: Tree Recursion
-------------------------------------------------
<xsl:function name="ex:fibonacci">
<xsl:param name="n" />
<xsl:choose>
<xsl:when test="$n eq 0">
<xsl:value-of select="0" />
</xsl:when>
<xsl:when test="$n eq 1">
<xsl:value-of select="1" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="ex:fibonacci($n - 1) +
ex:fibonacci($n
- 2)" />
</xsl:otherwise>
</xsl:choose>
</xsl:function>
I think it's unlikely that an XSLT implementor would optimize this to
avoid
the exponential nature of the algorithm. The reason is that it's
difficult
to find optimizations that would do this that are sufficiently general
to
optimize a significant number of real user-written programs - there's no
point in spending effort on optimizations that only help with
ivory-tower
examples.
The most realistic optimization that one might attempt for this function
is
to turn it automatically into a memo-function. That involves a
space-time
tradeoff; automating a choice that requires a space-time tradeoff
requires a
cost-based optimizer that understands the costs of space and time and is
able to estimate how much of either will be used. It's easy to look at
this
particular function and produce an argument as to why it should be
implemented as a memo-function. It's not at all easy to generalize that
reasoning so it can produce a good answer for any function.
Optimization techniques are advancing all the time (just look at the
advances with just-in-time approaches that benefit from learning about
the
behaviour of the code at run-time). But for this example, it's not there
yet.
I'd be interested how the performance of this compares with your other
implementation if you add saxon:memo-function="yes".
Regards,
Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail:
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--