[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: [xslt 2.0] Local functions

Subject: Re: [xslt 2.0] Local functions
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Thu, 19 Jul 2007 19:00:49 -0700
Re:  [xslt 2.0] Local functions
>Dimitre Novatchev:
>Having higher order functions (as provided by FXSL) eliminates the
>need for local functions. One can even create functions dynamically
>(by composition or by currying).

Dimitre, other than by composition or by currying, does FXSL support
arbitrary anonymous functions.

Hmm... Even Gmail shows me on the right hand side column the following "related page":


Dynamic Functions using FXSL: Composition, Partial Applications ... Dynamic Functions using FXSL: Composition, Partial Applications ... fxsl.sourceforge.net

and the more direct url is:

http://fxsl.sourceforge.net/articles/PartialApps/Partial%20Applications.html



I've tossed a poor man's lambda into my library which ends up
calling saxon:evaluate().

Using run-time evaluation is overkill here. A pre-processor would just do. Also, here the fact that XSLT is xml will come handy inboth analyzing the original stylesheet and in generating the result stylesheet -- this can all be done in XSLT.


Usage is something like this

s:map( s:l( 'some xpath expression which can operate on $p1'),
$sequence-of-values-id-like-to-translate-into-something-else)

e.g.

s:map( s:l( '$p1 * 2"), $x)

returns $x with all values in the sequence doubled.

Yes, you could do that in FXSL by currying.

Using FXSL one would just write:


f:map(f:mult(2), $seq)

and an example of the more general f:lift<N> () functions:

Given the following XML document:

<hexnums>
 <hexnum>A010</hexnum>
 <hexnum>60F0</hexnum>
</hexnums>

The following XPath expression

     f:lift2(f:add(),
              f:compose(f:hex-to-decimal(),f:first()),
              f:compose(f:hex-to-decimal(),f:second()),
              /*/*
              )


evaluates the sum of the two <hexnum> elements (which are first converted from hexadecimal to decimal)


Here is the definition of the f:lift2() function:



<xsl:function name="f:lift2"> <xsl:param name="pMainFun" as="element()"/> <xsl:param name="pFun1" as="element()"/> <xsl:param name="pFun2" as="element()"/> <xsl:param name="ptheArg"/>

   <xsl:sequence select=
    "f:apply($pMainFun, f:apply($pFun1, $ptheArg), f:apply($pFun2, $ptheArg))"
   />
 </xsl:function>

and the idea is to apply a 2-argument function on the results of two
other 1-argument functions when applied on a given single argument --
quite similar to functional composition, but the composition happens
along every argument of (in general) an N-ary function).


One can see another application of the lift functions in my one-liner solution to the fizz-buzz problem:

http://www.biglist.com/lists/xsl-list/archives/200703/msg00454.html


-- Cheers, Dimitre Novatchev --------------------------------------- Truly great madness cannot be achieved without significant intelligence. --------------------------------------- To invent, you need a good imagination and a pile of junk ------------------------------------- You've achieved success in your field when you don't know whether what you're doing is work or play


On 7/19/07, Justin Johansson <procode@xxxxxxxxxx> wrote:
>Michael Kay:
>Generally speaking, I think functions local to a function (or template) are
>not an especially good idea ...

>Dimitre Novatchev:
>As the need for local functions has never been expressed in this list
>for a period of 7 years, ...

Thanks guys, that was just the informed type of feedback I was seeking.

>Michael Kay:
> Anonymous functions would be a different question, if we had support
for higher-order functions in the language.

>Dimitre Novatchev:
>Having higher order functions (as provided by FXSL) eliminates the
>need for local functions. One can even create functions dynamically
>(by composition or by currying).

Dimitre, other than by composition or by currying, does FXSL support
arbitrary anonymous functions.

I've tossed a poor man's lambda into my library which ends up
calling saxon:evaluate().  Usage is something like this

s:map( s:l( 'some xpath expression which can operate on $p1'),
$sequence-of-values-id-like-to-translate-into-something-else)

e.g.

s:map( s:l( '$p1 * 2"), $x)

returns $x with all values in the sequence doubled.

Yes, you could do that in FXSL by currying.


Justin Johansson Now a Schema-Aware XSLT Evangelist

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.