[prev in list] [next in list] [prev in thread] [next in thread] 

List:       xsl-list
Subject:    Re: [xsl] Generate sequences of the permutations of a set
From:       "David Carlisle d.p.carlisle () gmail ! com" <xsl-list-service () lists ! mulberrytech ! com
Date:       2017-11-18 21:58:22
Message-ID: 20171118165750.78657 () lists ! mulberrytech ! com
[Download RAW message or body]

I'd probably avoid string operations until the end, this implements it
essentially as counting base $b except using digits 1-n not 0-n the,
then just replaces the numeric digits by the values from the elements
(the alphabet $a)


<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:n="data:n"
        exclude-result-prefixes="n"
>

 <xsl:template match="set">
   <xsl:sequence select="n:f(element/string(.),count(element),0)"/>
 </xsl:template>


 <xsl:function name="n:f">
  <xsl:param name="a"/>
  <xsl:param name="b"/>
  <xsl:param name="x"/>
  <xsl:if test="count($x) le $b">
   <sequence><xsl:sequence select="for $i in $x return $a[$i]"/></sequence>
   <xsl:sequence select="n:f($a,$b,n:add($b,$x))"/>
  </xsl:if>
 </xsl:function>

 <xsl:function name="n:add">
  <xsl:param name="b"/>
  <xsl:param name="x"/>
  <xsl:sequence select="if(empty($x)) then 1 else
            if($x[last()] lt $b)
            then ($x[position()!=last()],$x[last()]+1)
            else (n:add($b,$x[position()!=last()]),1)"/>
 </xsl:function>


</xsl:stylesheet>
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/651070
or by email: xsl-list-unsub@lists.mulberrytech.com
--~--
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic