Wednesday, January 03, 2007

Live Blogging a Javascript hackers first dive into Erlang

There comes a time in every developers life ,when you want to find out for himself ,what ,how and why the hell people swear by their languages .Especially when its not one that youve ever worked with . For me ,it started off with Paul Graham godfathering Lisp . No i havnt Lisp/scheme land yet .But i have instead gone into another one , inspired by the one paradigm that has got everyone from languages all over the place going gaga.

Functional Programming . And the language im talking about ........is Erlang.

Now being a javascript 'n DOM hacker myself, Ive always wanted to take on any claim that other framework combinations (read django,ruby on rails , pylon ,and the rest) have to well...claim as far as im concerned by quoting improved performance , rapid prototyping , and ease .

Do i think a well informed programmer javascript /DOM hacker is any less equipped to implement ANYTHING that a ruby on rails coder can ?

my answer is No. not yet anyway . Either that or i havent seen anything so exclusive to a ruby on rails that cannot be implemented otherwise .( Let the rants begin ..he he ... 8 P )


Coming back to Erlang .What im going to attempt to do is :
  • show the erlang example from the nice folks at erlang.org
  • rewrite the same in javascript ,
  • rewrite the same in javascript ,attempting a FP style.
erlang code for factorial
-module(tut1).
-export([fac/1]).

fac(1) ->
1;
fac(N) ->
N * fac(N - 1).

Execution
tut1:fac(4).
a modest javascript equivalent of erlang code
/**
* @author Bhasker V Kode
* @description a modest javascript equivalend of the erlang factorial example.
* @date Jan3 ,2007
*/
function fac1(number){
var total = 1;
for(var N=0 ; N <>


Execution:
alert(fac(4));
javascript equivalent of erlang code with a modest Functional Programming Style
/**
* @author Bhasker V Kode
* @description a modest javascript equivalend of the erlang factorial example.
* @note : oh erlang gods ! please let me know if this could have been more erlangish.
* @date Jan3 ,2007
*/
function fac(total,number){
if(number >1 ){
fac(total+number,number-1);
}else{
return total;
}
}


Execution:
alert(fac(1,4));

Now as the nice documentation said - what ive atttempted to do with this erlang program OR rather the highlight of functional style would be the limited need/scope of variables and instead the fact that the argument to a function can be the result of another function. Pretty wicked huh!

Now i know ive gone wrong somewhere . So if any erlanger / FP guru happened to see this ,they might know ?
  • if you cud make the js above with just one argument ?
  • if you should rather keep increasing the arguments being sent instead of increasing the sum
Lets move on to a more cryptic example of embedding functional style of programming into predominantly OOPs based javascript.

XmlWriter : a Javascritpt Class that uses minimalistic Functional Programming Styles

/**
* @author Bhasker V Kode
* @date Jan 4 ,2006
* @description Uused to create xml structures as a string ,and get a string form at any node level ,easily add nodes ,attributes and values.
*/

function XmlWriter(root){
this.totalNodes=new Number(0) ;

this.addNode = function(parent,nodeName,value){
if(nodeName!=undefined){
this.totalNodes++ ;
var len = this.index.length - 1 ;
this.index[this.totalNodes] = new Object() ;
if(value!=undefined){
this.index[this.totalNodes][nodeName] = new Node(nodeName,this.getDepth(parent)+1,value,this.totalNodes) ;
}else{
this.index[this.totalNodes][nodeName] = new Node(nodeName,this.getDepth(parent)+1,this.totalNodes) ;
}
this.index[this.getIndex(parent)][parent].array.push(nodeName) ;
}
}

this.getRoot = function(){
return this.root ;
}

this.getNode = function(parent){
var len = this.index.length ;
return this.index[len - 1 ][parent] ;
}
this.getIndexElem = function(id,assoc){
if(id < 0){
id=0 ;
}
return this.index[id][assoc] ;
}

this.printXML = function (node,str){
if(str==undefined){
var str = new String('') ;
}
var tempArrElem = this.getIndexElem(this.tempIndex,node) , tempArr = tempArrElem.array ;
// this.jsAlert('going to call start tag for this.index['+this.tempIndex +'having name '+ tempArrElem.name+' and attribs '+ tempArrElem.attribs) ;
str+= this.startTag(node,tempArrElem.attribs) + tempArrElem.value ;

if(tempArr.length > 0 ){
for(var j=0 ; j < tempArr.length ; j++){
//this.jsAlert('j='+j+',node :'+node+',this.tempIdex is '+this.tempIndex+'so tempArr is : this.index['+this.tempIndex+']['+node+'] = '+tempArr[j]) ;
this.tempIndex++ ;
// this.jsAlert('going to call printXML with node:'+tempArr[j]+',cuurent str is '+str) ;
str = this.printXML(tempArr[j],str) ;
}
// this.jsAlert('done with filled'+ node+'this.printed = ' + str) ;
str+= this.endTag(node) ;
return str ;
}else{
// this.jsAlert('done with empty'+ node+'this.printed = ' + str + this.endTag(node)) ;
str+= this.endTag(node) ;
return str ;

}

}

this.startTag = function(nodeName,attribs){
if(attribs==undefined){var attribs='' ; }
return ' < '+nodeName+attribs+'>'
}
this.endTag = function(nodeName){
return ' < /'+nodeName+'>'
}

this.getDepth = function(nodeName){
for(var i =0 ; i < this.index.length ; i++){
for(var node in this.index[i]){
if( this.index[i][node].name==nodeName){
return this.index[i][node].depth ;
}
}
}
}

this.getIndex = function(nodeName){
var tempRet=0 ;
for(var i =0 ; i < this.index.length ; i++){
for(var node in this.index[i]){
if( this.index[i][node].name==nodeName){
tempRet = i ;
}
}
}
return tempRet ;
}

this.getXML = function(nodeName){
if(nodeName==undefined){
var nodeName = this.root ;
}
this.tempIndex=0 ;
return(this.printXML(nodeName)) ;
}
//constructor
//adding root node
this.printed = new String() ;
this.index = new Array() ;
this.index[0] = new Object() ;
this.index[0][root] = new Node(root,0) ;
this.root = root ;

}

function Node(nodeName,depth,params,index){
this.array = new Array() ;
this.depth = depth ;
this.name = nodeName ;
this.index = index ;
if(params!=undefined){
if(params.value!=undefined){this.value=new String(params.value) ; }else{this.value=new String('') ; }
if(params.attribs!=undefined){
this.attribs=new String(' '+params.attribs) ;
}
}else{
this.value=new String('') ;
this.attribs=new String('') ;
}
return this ;
}

Execution:
var x = new XmlWriter('channels');
x.addNode(x.getRoot(),'channel',{attribs:'preferred="ndtv"'});
x.addNode('channel','shows');
x.addNode('shows','show',{attribs:'date="05:01:2006" title="waynes world"',value:'21:00:00'});
x.addNode('shows','show',{attribs:'date="05:01:2006" title="cnn news"',value:'22:00:00'});
alert ( x.getXML('channels') );
As you can see with the above example . This class can generate a consistent xml file from any particular node, handle attributes (although i need to add better error checking / escape character into the system) .The interesting part comes in the printXML function where ive implemented a minimalistic function style of programming where . Now ofcourse ive put in plenty of Object oriented javascript as evident with the use of the this keyword generously . But what ive also done is make the final printing of xml darn simple !

In essence my recursive function printXML does the following:

(XmlWriter.printXML in pseudoCode)

  1. write the start tag of this node .
  2. add the attributes
  3. if this node has a child node ? goto 1 , else goto 4
  4. write the end tag of this node

So now ....im convinced that javascript has done a good job in imbibing some tips from the erlang examples. The beauty of javascript is that since its pretty native as far as the browser is concerned ,you can write anything or implement any paradigm that isnt native to the langauge yourself . But as many wise ones have echoed over the years that the very fact that you code in a functional programming language style, could change the way you think about programming .

Cheers to my erlang and like my first experiment with functional programming ,the javascript hacker in me is up to the challenge as well 8 ) .

Keep Clicking,
Bhasker V Kode

PS : special thanks to Ricky Clarkson for hinting me to check the code again . i was coding the sum of N numbers although i started out wanting to do the factorial of N numbers ! d 'oH ! : P .... thanks ! I hate bloggers code formatting as well !

Technorati Tags: , , , , , , , , ,