Re: [xsl] Re: Pearls of XSLT and XPath 3.0 Design ... new chapter on Binary Search Trees

Subject: Re: [xsl] Re: Pearls of XSLT and XPath 3.0 Design ... new chapter on Binary Search Trees
From: Wolfgang Laun <wolfgang.laun@xxxxxxxxx>
Date: Wed, 9 Jan 2013 19:30:22 +0100
I'd like to add a variation on the topics of chapters 5 and 6. Now I
don't have an XPath 3.0 processor around, but I've written and tested
this in another language that has anonymous functions and closures,
and tried to translate it, to the best of my abilities.

The code below illustrates the combination of a closure to create a
recursive function, using Dimitre's two-stage approach to achieve the
recursive call via a previously assigned variable. The instantiation
of the final function is done by function "builder" that preserves the
essential parameters (acting as a closure). Two functions are bullt,
using the definitions of "isZero", "prod" and "sum".

<xsl:variable name = "builder" select = "
  function( $term as function(item()*) as item()*,
            $op as function(item()*, item()*) as item()*,
            $init as item()* ) as function(item()*) as item()*
  { let
    $h := function( $x as item()*,
                    $recurse as function(item()*,function()) ) as item()*
          { if( $term($x) ) then $init
                            else $op( $x, $recurse( $x - 1, $recurse ) )},
    $f := function($x as item()*) as item()*
          { $h( $x, $h ) }
    return $f" />

<xsl:variable name = "isZero" select = "
  function($x as item()*) as item()*
  {$x eq 0}"/>

<xsl:variable name = "prod"  select = "
  function($x as item()*, $y as item()*) as item()*
  {$x*$y}"/>

<xsl:variable name = "sum"  select = "
  function($x as item()*, $y as item()*) as item()*
  {$x + $y}"/>

<xsl:variable name = "faculty" select = "builder($isZero, $prod, 1 )"/>
<xsl:variable name = "series"  select = "builder($isZero, $sum, 0 )"/>

-W

On 08/01/2013, Costello, Roger L. <costello@xxxxxxxxx> wrote:
> Hi Folks,
>
> Dimitre and I collaborated to add a new chapter in the paper, "Pearls of
> XSLT and XPath 3.0 Design."
>
> The new chapter describes Dimitre's binary search tree implementation.
>
> Here are the first couple of paragraphs:
>
> CHAPTER 7: BINARY SEARCH TREES
> This chapter contains code for creating and processing binary search trees.
> The code is implemented entirely in XPath. An advantage of an "XPath-only"
> solution is that it can be used (hosted) in many programs--it can be hosted
> in XSLT programs, in XQuery programs, and in any other programming language
> that hosts XPath. Thus "XPath-only" solutions are highly portable and
> reusable.
>
> The XPath code inputs data from an XML document and stores the data into a
> binary search tree. By storing the data in a binary tree there are many
> operations that can be efficiently performed which would otherwise be
> difficult or inefficient.
>
> More ... http://www.xfront.com/Pearls-of-XSLT-and-XPath-3-0-Design.pdf
>
> Dimitre Novatchev and Roger Costello

Current Thread