Archive for November, 2007

Jupiter

Today I stumbled upon this really interesting, well underbuilt article about a (hypothetical) nuclear explosion on the surface of Jupiter in the spring of 2003, caused by the plunge of Galileo spacecraft (which contained plutonium fuel cells) on Jupiter’s surface. One month after Galileo’s final descent, a Belgian (jeej!) astronomer discovered a black spot on the giant gass planet, as you can see on the picture above. Reason for this black spot and for the one month time delay can be found on this page!. I’m not much of a scientist myself but this is a really well writen and understandable article, I’m not saying I’m convinced this has actually happened, but he does make it sound like there’s no question about it!

Full article here: http://www.enterprisemission.com/NukingJupiter.html

Dominiek

Strategy 101 for Microsoft.

Touchlight (Looks weird doesn’t it:p)

Recently, I’ve fallen in love with Apple’s new flagship, the Ipod Touch. Now before urging to the closest store to buy one, I did some reading on it and immediately discovered some flaws. The biggest fallback was the simple fact the the Touch hasn’t got any support what so-ever for Adobe Flash, and looks like this might be a very hard feature to implement.

This is where Microsoft could kick in and take advantage: Silverlight (Microsoft’s Flash ‘killer’) is already running fine on Linux and MAC and with some serious man hours of the Microsoft developement team, I have no doubt they could port Silverlight to the Apple Touch. If Microsoft would actually do this (if legal? :-) ), this could launch Silverlight into a broader audience and convert a whole bunch of none believers to take a closer look at Microsoft technology, eventually using it to construct other (non Touch) websites. It’s clear Microsoft’s Zune(2) can’t beat the Apple competition (or rather dominance), so why not try this approach?

Dominiek

Lambda Expressions in C# 3.0

One of the most powerful techniques I saw during our Scheme lessons was the ability to pass (and create) functions using lambda expressions. In Scheme (a LISP dialect…), procedures are treated as ‘first class objects’, a quick example:

>>> (define (map proc a-list)
(cond
((null? a-list) ‘())
(else
(cons (proc (car a-list))
(map proc (cdr a-list))))))

>>> (map (lambda (x) (* x 3))
(list 1 2 3 4 5))
(3 6 9 12 15)

We’ve declared a procedure map which will take every element of our list and apply the function ‘proc’ to each element of this list. Then we call our ‘map’ function on a list with the lambda expression (x) => x * 3 as the argument (that’s right, a function as an argument…).

I was curious if something like this could be done in C#, first I looked into delegates but I discovered the new C# 3.0 specification contains ‘lambda expressions’, or ‘anonymous methods’ in .NET 1.1. Here’s the Scheme example translated into C# 3.0 (using Visual Studio 2008 beta 2) with the lamda expression in the builtin generic Func type:

class Program
{
static List MapOverCollection(ICollection collection, Func func)
{
//We’ll pass the results as a list which we instantiate here
List result = new List();

foreach (T el in collection)
//Apply our lambda expression to each element in the ICollection
//and add it to our result list…
result.Add(func(el));

return result;
}

static void Main(string[] args)
{
//Declare a simple list with some elements
List testList = new List();
testList.Add(1);
testList.Add(2);
testList.Add(3);
testList.Add(4);
testList.Add(5);

//Print our list to check it’s contents
PrintList(testList);

//Apply our lambda expression to each element of the list
//using our generic method.
List mappedList = MapOverCollection(testList, i => i * 3 );

//Print the result list.
PrintList(mappedList);

Console.Read();
}

static void PrintList(List list)
{
Console.Write(”(”);
foreach (T el in list)
Console.Write(” ” + el.ToString() + ” “);
Console.Write(”)\n”);
}
}

The output is the same! Now you might ask: “Why should we use this?”. Well, seems like Msft. has included an armada of selectors which take predicates (for instance x => x > 5) as arguments, a small example of this is the where method on an IEnumerable:

IEnumerable overFive = someNumbers.Where(i => i > 5);

This will make a new IEnumerable which contains all elements of the someNumbers IEnumerable that are larger than 5. Offcourse you can also apply this kind of logic on your own classes (Person, Download, …) or other builtin classes (strings, listviewitems, …).

To finish this post I’d like to show an alternative way of event handling using the lambda expressions. Suppose you’d want to popup some text when a user clicks a button, normally you’d attach event handlers and implements the appropriate methods. Now you could use lambda expression, like this:

btnLambdaButton.Click += (sender, args) => MessageBox.Show(”Clicked”);

We attach an eventhandler using the += operator, we create a lambda expression with the two (not possible in normal lambda calculus) usual arguments for an event (it’s sender and the eventargs) and after the lambda operator (=>), we say what should be done… That’s it!

These lambda expressions don’t make life easier for the average developer, but once you get the hang of it I’m sure it’s a superb tool for every coder. Offcourse one can argue that lambda expressions don’t belong in an OO language such as C# but that such academic features should remaing solely in functional languages (such as Scheme). But if you don’t like it, nothing is stoppig you from shoving it in the closet!

Dominiek

Iteration vs. recursion in C#

Recursion

Today I found a short yet interesting article on codeproject (which is the place to be if your interested in the latest C# developments) on recursion versus iteration. It explains that it should be common practice to use iterative methods instead of recursive, especially because of possible stack overflows. If you can’t avoid recursion, the article also explains a technique called “memoization” (without the ‘r’ :-) ). This is basically a key-value collection (such as a generic dictionary) that remembers the result of a previous function call so the output doesn’t need to be calculated again.

An excerpt:

N Recursive Iterative
10 334 ticks 11 ticks
100 846 ticks 23 ticks
1000 3368 ticks 110 ticks
10000 9990 ticks 975 ticks
100000 stack overflow 9767 ticks

The full article by Eyal Lantzman is available here

Yesterday I accidentally downloaded about a thousand files from a user on Soulseek. The program went completely nuts and just crashed when I tried to remove them from the queue (work needs to be done here by the soulseek devs…). I couldn’t start Soulseek anymore so I had to go look for the queue database. Luckily I wrote a small program a couple of months ago that can monitor creation and changes of files which showed me it was the file “queue2.cfg” in the directory “C:\Users\Dominiek\AppData\Local\VirtualStore\Program Files\Soulseek-Test” I had to remove. With this done, Soulseek worked “fine” again! Note that this happened on Vista Home Premium, although I remember similar stability problems on XP.