Re: References in XPath that can be represented by a stringandstill can be garbage collected

"Vladimir Nesterovsky" <[email protected]> Mon, 22 Dec 2008 09:21:45 +0200
Newsgroups gmane.text.xml.xslt.extensions
Message-ID <[email protected]>
>> It seems to me that the idea of explicit "Resource Manager"
>> object goes against the spirit of functional programming.
>>
> The concept of a "resource manager" does have the merit that it provides a
> scope for the persistence of string references:

Then isn't it simpler to introduce a mutable reference and allow statefull 
logic like in this pseudo code:

  <xsl:function name="t:create-generator" as="item()">
    <!-- Creates a mutable reference and binds initial value. -->
    <xsl:variable name="state" as="item()" select="f:mutable-ref(0)"/>

    <!-- generator function. -->
    <xsl:variable name="generator" as="function() as xs:integer">
      <xsl:function as="xs:integer">
        <!-- Get refered value, and update it. -->
        <xsl:variable name="value" as="xs:integer" select="deref($state) + 
1"/>

        <!-- Rebind a reference. -->
        <xsl:sequence select="f:rebind($state, $value)"/>

        <xsl:sequence select="$value"/>
      </xsl:function>
    </xsl:variable>
  </xsl:function>

  <xsl:function name="t:next" as="xs:integer">
    <xsl:param name="generator" as="function() as xs:integer"/>

    <!-- Get the next value. -->
    <xsl:sequence select="$generator()"/>
  </xsl:function>

Function t:next() has about the same side effect as a function returning a 
value derived from node identity.

A mutable reference allows to model a "resource manager".
--
Vladimir Nesterovsky
http://www.nesterovsky-bros.com