[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Using divide-and-conquer recursion to create a cu

Subject: Re: Using divide-and-conquer recursion to create a cumulative sequence
From: Andrew Welch <andrew.j.welch@xxxxxxxxx>
Date: Sat, 12 Dec 2009 10:11:01 +0000
Re:  Using divide-and-conquer recursion to create a  cu
2009/12/11 Costello, Roger L. <costello@xxxxxxxxx>:
>
> Hi Folks,
>
> I wish to convert a sequence of N numbers:
>
>   (23, 41, 70, 103, 99, 6)
>
> Into a cumulative sequence, in which each number is the sum of the previous
numbers:
>
>   (23, 64, 134, 237, 336, 342)


Hi Roger,

Here's a function that recursively processes sequence, with linear
performance:


<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:f="f"
    exclude-result-prefixes="xs">

<xsl:variable name="seq" select="(23, 41, 70, 103, 99, 6)" as="xs:integer+"/>

<xsl:function name="f:sumSeq" as="xs:integer+">
	<xsl:param name="seq" as="xs:integer+"/>
	<xsl:param name="pos" as="xs:integer"/>

	<xsl:sequence select="if ($pos gt count($seq)) then $seq else
		f:sumSeq((subsequence($seq, 1, $pos), $seq[$pos + 1] + $seq[$pos],
subsequence($seq, $pos + 2)), $pos + 1)"/>
</xsl:function>

<xsl:template match="/">
	<xsl:value-of select="f:sumSeq($seq, 1)"/>
</xsl:template>

</xsl:stylesheet>


cheers
--
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.