xsl-list
[Top] [All Lists]

Re: [xsl] [xslt 2.0] Local functions

2007-07-19 19:01:09
>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(_at_)tpg(_dot_)com(_dot_)au> 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

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

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