kenta blog

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? ;)

martedì, febbraio 10, 2009

[jsn] isNative: a.k.a. the windmill war

Long long time ago me and Andrea Giammarchi have nothing better to do to understand if there was a way to understand if you were using a safe function in javascript or if you use a fake one.

We try very hard to make something reliable, and without fail we manage to crack every single code we wrote.

I gave up but since then every now and then I recall that days in which we enjoyed this "windmill war".
Since when I gave up I had an idea, I just tried to see if that I can work out something to make that idea work.

What IDEA?


Basically since we cannot know if a function is real I thought to make the browser fail if I try to eval some code, make the eval code call himself in an infinite loops.

<script type="text/javascript">
eval = function(str){
eval(str);
}

eval("alert('hello, proof');"); //too much recursion on FF, stack overflow on IE ;)
</script>


Basically if I extend this logic to all the string evaluating function
(Function object, eval, Object.eval, setTimeout, setInterval) made them using only one function It should work.

example:

eval = function(){
return function(code){
(new Function(""+code))();
}
};


in this way if I try to use eval to rewrite Function I just can't because Function is called inside eval for the same principle of the first snippet I posted.

So why a windmill war?


Caching.
Function caching to be precise,
in javascript I can write this code:

var f = eval

eval = function(str){
alert("I' m an evil functions! bwahahaha!");
f(str);
}


So I don't need to use Function anymore to rewrote eval, I just Spoof the code,
I can send The spoofed code around using http request (for example a src in images...).

To recap, bear this in mind:
Javascript is so powerful in the way it leaves you modify its behaviour that is totally unreliable.

domenica, novembre 16, 2008

[jsn] IE Javascript REAL speed tester

After reading the John Resig post about javascript time accuracy, I decided to make a simple IE wrapper that expose throught the window.external interface an high resolution timer for testing IE Javascript performance

The project is a very basic .net 3.5 window form project written using VS 2008 express,
it will require .net 3.5 installed.

I released the project on codeplex, you can found it at http://www.codeplex.com/IEJst

Etichette: , , ,

lunedì, novembre 10, 2008

AVG Aggiornamento kamikaze del 9/11/2008 ripristino XP

Ieri Avg ha rilasciato un aggiornamento a dir poco kamikaze, infatti per un falso positivo, Avg rileva la user32.dll come malevola ed affetta da un trojan ... e chiede di rimuoverla, ovviamente una buona parte di utenti si è trovato a dir : "Sì, rimuovila pure, grassie!"

...

Poi al riavvio del pc non son più riusciti ad accedere ad XP (...chissà come mai...)

Allora vediamo di far notare con chiarezza la cosa:
Se AVG vi dice che il file user32.dll è infetto da "PSW. banker4.APSA" (o qualcosa di simile) non cancellate, ne spostate nulla!!!

Il mio consiglio è di passare ad Avira free (http://www.free-av.com/) che si è dimostrato recentemente migliore di avg / avast, se invece volete rimanere ad avg mettete il file c:\windows\system32\user32.dll tra le eccezioni dell' antivirus...

Ho inavvertitamente cancellato user32.dll e non mi parte il pc, e adesso???

Allora, semplicemente prendete il cd di installazione di XP (dovete averlo ;) ) e inseritelo, assicuratevi che il vostro BIOS permetta l'avvio da CD/DVD rom e riavviate il pc.

Vi verrà chiesto se volete avviare da cd, premete invio per proseguire
aspettate un po che carichi il menù, ad un certo punto vi verrà chiesto qualcosa,
voi premete R per entrare in console di riparazione e ripristino
a questo punto dovrebbe venirvi scritto qualcos' altro, non ricordo voi continuate finché uscira una roba con dei numeri, nella maggior parte dei casi premete 1 (la spiegazione è sicuramente troppo tecnica)

bene
ora guardate, quello è un prompt XD
ammiratelo con sacro rispetto per 2 minuti dopodiché digitate (premendo invio ogni comando)


D:

dir /ad


se tra le cose che vi vengono scritte c'è un I386 proseguite al punto G
altrimenti provate scrivendo


E:

dir /ad


se prima non c'era qui dovrebbe esservi un I386, altrimenti andate avanti con l'alfabeto finché troverete la prima I386 dopodichè potete anche voi passar al punto "G"


Punto G
a sto punto scrivete (ricordo sempre l'invio)


cd I386

expand user32.dl_ c:

copy c:\user32.dll c:\windows\system32


da notare l'underscore (il trattino di sottolineatura) nel comando expand

bene ora potete riavviare e una volta fatto potete (S)cancellare il file user32.dll in c:\ (non quello in c:\windows\system32 però)

se AVG vi tedia ancora dicendo che c'è un virus fate la famosa "finta da pomi" (cioè ignoratelo)

Etichette: , , , ,

martedì, settembre 30, 2008

Vi presento un nuovo recensIONISTA di giochi! :D

Ho iniziato una collaborazione con il sito Gameszone.it, per cui scrivo per loro alcune recensioni di giochi che provo in anteprima :D
I primi 2 giochi che ho avuto in carico di recensire sono S.T.A.L.K.E.R.: Clear Sky e X3: Terran Conflict.
Il primo è il prequel molto atteso di S.t.a.l.k.e.r.: Shadow of Chernobyl, che è un FPS con alcuni elementi RPG di stampo survival horror, il secondo invece è una simulazione di vita nello spazio, che in realtà è anch' essa a sua volta un seguito, in particolare trattasi di un' espansione standalone di X3: Reunion;
X3 mi ricorda per certi versi un gioco del '94 dove si colonizzava lo spazio (che volevo fare lo sborone citandolo, ma che in questo momento non mi sovviene il nome) comunque mentre quello era un gioco strategico a turni questo è tutto in real time, ma real time vero, tutto calcolato in tempo reale.

Entrambi i giochi sfoggiano una grafica mostruosa, e sono pure avidi di risorse, Stalker al massimo dettaglio fa fare fatica al mio nuovo PC (un quadcore con 4gb di ram e una 8800gt!!!) comunque, giochi a parte, per me è stata un esperienza nuova, vedere pubblicate le proprie recensioni dà una soddisfazione senza eguali, è stata una gioia dedicarmici e spero in futuro di poterlo fare sempre più, vi invito,
se vi interessa, a leggere le recensioni di S.T.A.L.K.E.R.: Clear Sky e di
X3: Terran Conflict
e di lasciare, in caso, qualche commento qui, suggerimenti, critiche, idee eccetera tutto è bene accetto.

Ps. Questa piccola avventura ha già portato i suoi frutti, infatti sto migliorando il mio modo di scrivere, che era arrugginito dalla sindrome SMS / Chat.
Risulta infatti difficile scrivere un qualcosa correttamente dopo essersi (MALE!) abituati alla scrittura veloce da messaggini, mail e sms vari, son però contento perché tutto torna utile :D

Etichette: , , ,

martedì, luglio 08, 2008

Dustin Diaz Programming Brain Teaser

TEST : http://www.dustindiaz.com/programming-brain-teaser/

A solution:

arr.join("")
.replace(/(.)(\1)((\1)+)/g,'$1$1[$3]')
.split("")
.join(" ")
.replace(/\[\ /g,"<span>")
.replace(/\ \]/g,"</span>")


obviously it can be simplified, but for me that's enought ;)

domenica, maggio 04, 2008

pensavo di scrivere male... e invece!

Questo è il risultato del test applicato sul mio blog,
è un test di leggibilità, come potete vedere per leggere il mio blog non occorre essere indottrinati :D, buon per voi, :lol:!

blog readability test

Etichette: