Saturday, September 29, 2007

using arguments vs. an array parameter

This goes for both actionscript and javascript and I guess any language that lets you create functions without an arbitrary number of parameters (a syntax known as elipsis in some other languages).

It is very temping to use this syntax to write general functions such as Max or Min with an arbitrary number of parameters.

you would get something like (and this just an outline not code designed to work).

function maxOfN() {
var m=arguments[0];
for (var i=1;i if (arguments[i]>m) m=arguments[i];
return m;

you can then use this in a clean way:
var m1=maxOfN(4,8,3,9);

So what is the alternative?
Work on an explicit array of values

function maxOfN(arrValues) {
var m=arrValues[0];
for (var i=1;i if (arrValues[i]>m) m=arrValues[i];
return m;

and you get the less clean syntax:
var m1=maxOfN([4,8,3,9]);

so it looks as if its more "cool" to use the first option
but as I found out after writing some "cool" code there is a minor problem with this approach:

suppose you already have an array with the values you want to work on (as I did),
you will then have to use:

// values is set to an array of values by previous code
mar m1=maxOfN.apply(null,values);

0) Assumes you even have an apply in your language
1) Looks much worse then
var m1=maxOfN(values);
2) Actually means that you build an arry in your code, then call apply which takes the array appart, pushes it onto the stack just so that you can as for the arguments array and have all the parameters pushed back into the array you could have passed directly to begin with.

So my recomandation would be to pass the array explicitly and pay for the creation of the array parameter

var m1=maxOfN([4,8,3,9]);

This also has the advantage of being able to pass along more then a single array to a function and not being limited to having it as the last parameter of the function.

consider somthing like:
var sums=inPairs([1,3,5],add,[4,6,8]);
var products=inPairs([1,3,5],mult,[4,6,8]);

Anyone wants to guess what I would expect sums and products to be ?
Did i hear any one say "Excel" or "Array functions" ...

Thursday, September 06, 2007

Luciano Pavarotti died, God rest his soul.

71 yeas old Luciano Pavarotti,
one of the world famous 3 Tenors
died today.

Luciano Pavarotti on IMDB

Saturday, September 01, 2007

virtual pc "Use Physical Drive" grayed out

A few hours ago I downloaded virtual pc 2007 and decided to try my new vista ultimate.

The download and install of virual pc was quick and easy, and a very short time afterwards I had a virtual machine ready for the installation.

Well, as far as I could tell, the next thing to do was to put the vista cd into the DVD and boot the VM.

That didn't seem to work that well.
What am i missing? hmm...

A quick look at the PVC2007 menu and I saw I have a "Use Physical Drive F:" in there (which is my DVD) but it was disabled.
hmmm... again.

Another quick look and I see a CD icon at the bottom.
Now that took me for a short ride down the settings of the VM where I had the option of choosing between my primary or secondary IDE controller.
Nither made things better.

Well, It took me about another hour until I realized what was the real problem:
My original genuine fresh out of the litle plastic box with all the holograms and stickers aroundit vista ultimate dvd was just not working.
Had nothing to do with virtual pc.
but hey, it would be nice of VPC said something along the lines of "No media found" or something like this, and not just give me the impression that the problem is with not being able to connect to the drive itself.

I hope that if you are reading this it saved you some time,
go ahead, check your media - does it work on your host machine !?

(b.t.w. - I got the vista together with some more MS software - office 2k7 and such,
so when I found out that the vista DVD was f$&ked up I went along and checked all the other DVDs. No problems there... it was just the two copies of vista. I guess they come from some faulty batch. Now I have to contact MS to get a replacement ...)

Clicky Web Analytics