xsl-list
[Top] [All Lists]

Re: [xsl] XPath 3.0 How to implement the function composition operator?

2012-10-15 22:17:34
Roger and Michael,

Here is the definition of the compose() function I was after --
working with BaseX:

let  $apply := function($f as function( item()*) as item()*,
                        $x as item()*
                        ) as item()*
                {$f($x)},

     $_comp := function($f as function( item()*) as item()*,
                        $g as function( item()*)  as item()*,
                        $z as item()*
                      )
                      as item()*
             {
              $apply($f, $apply($g, $z))
             },

     $comp := function($f as function( item()*) as item()*,
                       $g as function( item()*)  as item()*
                      )
                     as function( item()*) as item()*
             {
               $_comp($f, $g, ?)
             },

     $double := function($m as item())  as item()
                { $m *2 },
     $incr   := function($x as item()) as item()
                { $x + 1}

 return
     $comp($double, $incr) (30)


This produces thecorrect, expected result:

62



On Mon, Oct 15, 2012 at 5:56 PM, Dimitre Novatchev 
<dnovatchev(_at_)gmail(_dot_)com> wrote:
I thought that using the argument placeholder "?" could be used to
specify a more readable implementation.

However it seems tht Saxon EE 9.3.05 (coming with oXygen) doesn't
support argument place holders.

For this query:

         let $f := function($m as xs:integer, $n as xs:integer) as xs:integer
                         {$m + $n}
           return
               $f(5, ?)(3)

an error message is raised:

Unexpected token "?" in path expression
Start location: 24:0
URL: http://www.w3.org/TR/xpath20/#ERRXPST0003

Could someone, please, explain what is the issue with this expression?


Cheers,
Dimitre



On Mon, Oct 15, 2012 at 4:02 PM, Michael Kay <mike(_at_)saxonica(_dot_)com> 
wrote:
compose is a function that takes two functions as input and produces a third
function as output, so it looks like this:

$compose   :=  function($a as function(item()*) as item()*,

                        $b as function(item()*) as item()*)
                     as (function(item()*) as item()*)
{  function($c as item()*) as item()* { $b($a($c)) } }

(Or the other way around. I don't know which way Haskell does it.)

Michael Kay
Saxonica



On 15/10/2012 23:08, Costello, Roger L. wrote:

Hi Folks,

How is function composition implemented in XPath 3.0?

Example: Suppose I want to compose these two function:

     1. increment: this function increases its argument by 1.

     2. double: this function multiplies its argument by 2.

In Haskell I can compose the two functions like so:

     f = double . increment

And then I can apply the composed functions to an argument:

     f 2

The result is 6.

How is f implemented in XPath 3.0?

Here is my attempt, which is not correct:

             let $increment :=  function($x as xs:integer) {$x + 1},
                  $double        :=  function($y as xs:integer) {$y * 2},
                  $compose   :=  function(
                                                                $a as
function(item()*) as item()*,
                                                                $b as
function(item()*) as item()*
                                                             )
                                                            as item()*
                                                {$b($a)},
                 $f  :=  $compose($double, $increment)
             return $f(2)

What is the correct way?

/Roger

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




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




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



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