Subject: Re: [xsl] [xslt 2.0] Local functions From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx> Date: Thu, 19 Jul 2007 19:00:49 -0700 |
>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
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.
<hexnums> <hexnum>A010</hexnum> <hexnum>60F0</hexnum> </hexnums>
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)
<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:
-- 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
>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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] [xslt 2.0] Local function, Justin Johansson | Thread | Re: [xsl] [xslt 2.0] Local function, Justin Johansson |
Re: [xsl] [xslt 2.0] Local function, Justin Johansson | Date | Re: [xsl] [xslt 2.0] Local function, Justin Johansson |
Month |