mercoledì, luglio 08, 2009

So, I want to learn at least a functional languag....hey, wait! I already know it! XSLT!

So, latelly on the web I've read about functional languages and how they will simplify the way to write complex multithreading/multicore programs, so I think that for be a good developer I need to learn at least one functional language, just for understand what is all this about.

WHY FP?
The bigger benefit from Functional Programming is that since in this style of programming the output depends only from the input parametesr, function have no side effects, and this allow simpler multithreading.

HOW FP WORKS?
Basically you describe a set of function ad tell the function what output give based on its input parameters.


but, hey! wait! if in the last sentence you substitute set of function with set of templates and input parameters with matches you can get that the concept here is very similar of how XSLT works...

In fact, in xslt a given template when feeded with the same input always produce the same output!
Xslt,by the way, under the hood use multithreading so seems that xslt have a lot in common with functional programming language...

mmm...

So, to recap, you can say that XSLT templates are the equivalent of FP functions...
... not quite really, since XSLT templates can't be passed across templates by refence ...

TILL...

... I found this paper !

The examples are not very clear (and font-size are weird on that site), so I write a little Example XSLT here:

<?xml version="1.0"?>
<!-- Here I define a defn namespace, we will need it to define our function-->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:defn="my-functional-xslt-example"
>

<xsl:output method = "text" />

<!-- here I will define 2 functions add3 and add4 -->

<defn:add3 />
<xsl:template match="defn:add3">
    <xsl:param name="parameter" />
    <xsl:value-of select="3+$parameter" />
</xsl:template>

<defn:add4 />
<xsl:template match="defn:add4">
    <xsl:param name="parameter" />
    <xsl:value-of select="4+$parameter" />
</xsl:template>

<!-- ok, now I will define a function that accept a reference of another function and execute that -->
<xsl:template name="execute">

<xsl:param name="function" select="/.."/>
<xsl:param name="parameter" />

<xsl:variable name="result">
    <xsl:apply-templates select ="$function" >
        <xsl:with-param name="parameter" select="$parameter" />
    </xsl:apply-templates>
</xsl:variable>

<xsl:value-of select="$result" />
</xsl:template>

<xsl:template match="/">

<!-- ok, now I will obtain the reference of our function, and I will store that in two variables -->
<xsl:variable name = "add3" select = "document('')/*/defn:add3" />
<xsl:variable name = "add4" select = "document('')/*/defn:add4" />

<!-- and this is how call the function passing add3 -->

<xsl:call-template name="execute" >   
    <xsl:with-param name="function" select="$add3" />
    <xsl:with-param name="parameter" select="10" />
</xsl:call-template>

<!-- and this is how call the function passing add4-->

<xsl:call-template name="execute" >   
    <xsl:with-param name="function" select="$add4" />
    <xsl:with-param name="parameter" select="20" />
</xsl:call-template>

</xsl:template>

So seems simple (but verbose) to pass around function references.

You will argue that this example works only on MSXML, but on the FXSL page you will found the saxon / xalan corrispondence.

on msxml you can obtain a similar results using msxml:script but this require a little javascript code and JSCRIPTxxx.dll and ,for example on pda, this could be a issue.

So, at the end I can say I already know at least a functional programming language... a sort of... or not? ;)

Nessun commento: