xsl-list
[Top] [All Lists]

Re: [xsl] XPath 3.0: Is it possible to do recursion in an anonymous function?

2012-10-14 12:32:32
Using Dimitre's technique, I implemented the "until" function:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                            xmlns:xs="http://www.w3.org/2001/XMLSchema";
                            version="3.0">

    <xsl:output method="text"/>

    <xsl:variable name="isNegative" select="function($x as xs:integer) {$x lt 
0}" />
    <xsl:variable name="decrement" select="function($x as xs:integer) {$x - 
1}" />
    <xsl:variable name="_until" select="
                  function(
                                    $p as function(item()*) as xs:boolean,
                                    $f as function(item()*) as item()*,
                                    $x as item()*,
                                    $_until as function(function(), 
function(), item()*, function()) as item()*
                                 ) as item()*
                     {if ($p($x)) then $x else $_until($p, $f, $f($x), 
$_until)}
        " />

    <xsl:variable name="until" select="
                  function(
                                     $p as function(item()*) as xs:boolean,
                                     $f as function(item()*) as item()*,
                                     $x as item()*
                                  ) as item()*
                      {$_until($p, $f, $x, $_until)}
          " />

    <xsl:template match="/">
        <xsl:value-of select="$until($isNegative, $decrement, 3)" />
    </xsl:template>

</xsl:stylesheet>

/Roger



Nice, Roger,

The naming of the last argument makes the code *look* like ordinary recursion.

And this could deceive some people! :)  Also, the name of this
argument will have to change for a new function to match the function
name.


I would personally prefer a more standard and unchangeable name, such
as "_step".

To go further, the type of the  "_step" argument could be specified as
function()+ .

This could be used to implement "non-deterministic" functions, where
at each step, the next processing step is randomly determined from the
available choices ($_step functions).

I feel overwhelmed at what can be accomplished with pure XPath 3.0
anonymous functions.

-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.

--~------------------------------------------------------------------
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>
--~--

<Prev in Thread] Current Thread [Next in Thread>