Re: [xsl] Seeking an elegant XSLT function to check that a pair of numeric ranges are consecutive

Subject: Re: [xsl] Seeking an elegant XSLT function to check that a pair of numeric ranges are consecutive
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 26 May 2024 16:53:11 -0000
The requirement asked for elegance, not efficiency.

And this is the way we identify new optimization opportunities:

(a) (1 to n, n+1 to m) ==> 1 to m.

(b) deep-equal(a to b, x to y)  ==> a=x and b=y.

MK

> On 26 May 2024, at 16:31, Dimitre Novatchev dnovatchev@xxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
>
>
> On Sun, May 26, 2024 at 7:15b/AM Roger L Costello costello@xxxxxxxxx
<mailto:costello@xxxxxxxxx> <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx
<mailto:xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>> wrote:
>> Another mind-blowing solution. This one from Michael Kay:
>>
>> <!--
>>     Determine if the ranges in $seq are consecutive.
>>     $seq = (0-2, 3-7, 8, 9-15) returns true
>>     $seq = (0-3, 3-7, 8, 9-15) returns false
>> -->
>> <xsl:function name="f:isConsecutive" as="xs:boolean">
>>     <xsl:param name="seq"/>
>>
>>     <xsl:variable name="seq-with-to" as="xs:integer*">
>>         <xsl:evaluate xpath="$seq ! replace(., '-', ' to ') =>
string-join(',')"/>
>>     </xsl:variable>
>>     <xsl:sequence select="deep-equal($seq-with-to, $seq-with-to[1] to
$seq-with-to[last()])"/>
>> </xsl:function>
>
> Not very efficient, though.
>
> Try it with: (1-10000000, 10000001-20000000, 20000001-30000000, 30000001)
>
> Then run the pure XPath solution (provided earlier in this thread) - it
takes 0.0s (as per Oxygen) to execute.
>
> The above XSLT transformation takes 5.2s (as per Oxygen) to produce the same
result.
>
> Anyone, please try these for yourself.
>
> Thanks,
> Dimitre
>
>
>
>
>
>
>
>
>>
>>
>
>
> XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
> EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/293509> (by
email <>)

Current Thread