15 January 2010

Web dev

I've come back from Aberdeen with a lovely newsecond hand Guitar! which I love waaaay too much.

Anyway, that whole thing rather distracted me from my business plans here, wasting ovar nine thousand man-hours.

Back to coding though, today: and you know I don't like to make a half-assed job of things (unless I'm hacking in the pub!)

So I wanted to share this little javascript function of which I'm slightly proud, mostly because I implement half the function haskell list library within it.

// From a list of lists of two elements
// Create application/x-www-form-urlencoded parameters
function params(ps)
{
if (ps == undefined)
{
return "";
}
else
{
var map = function(l,f)
{
var a = [];
var i;
for(i=0; i<l.length; i++)
{
a[i] = f(l[i]);
}
return a;
};
var fold = function(l,s,f)
{
var i;
for(i=0; i<l.length; i++)
{
var s = f(s, l[i]);
}
return s;
}
var intersperse = function(i,l)
{
var f = function(li,e)
{
li.push(i);
li.push(e);
return li;
}
var l = fold(l,[],f);
l.splice(0,1);
return l;
}
var join = function(ll)
{
return fold(ll,[],function(o,l)
{
return fold(l,o,function(oo,e)
{
oo.push(e);
return oo;
});
});
}
// Pair the inputs
var ps_pairs = map(ps,function(p)
{
return p[0] + "=" + p[1]
});
var amps = intersperse("&",ps_pairs);
// join them together
var joined = join(amps).join("");
return joined;
}
}


Excessive?! No!!! But really what I need to do is to remove the inner functions into an array processing library of their own... I think I might make a couple of medium sizish javascript libraries for various things...

No comments:

Post a Comment