Saturday, June 28, 2008

It's Raganwald's fault this time...

I just spent a day trying to write a program to solve Raganwald's puzzle (at the end of the article). Until now I thought I was a very intelligent guy. Tough :P

At least I added a few more extension methods to my tools class :)

Friday, June 27, 2008


As far as I know, I've always been a proponent of "worse is better" - I favor "cheap and bad" over "expensive and good". I don't wear brand clothing, I don't use brand appliances and I definitely hate brand computers.

The price/performance ratio is one reason.

Case A. One $100,000 computer with a guaranteed up-time of 99.9999% - that's less than an hour of downtime in a year. (I sincerely believe that any actual computer with such a guarantee would cost a lot more than that.)

Case B. Several computers with a guaranteed up-time of 90% - that's one day of downtime in ten, or more than a month of downtime in a year.

How many computers are needed to obtain the same reliability as the first system?

Well... the chance of one such system failing is 1 - 90% = 10%. The chance of two of them failing at the same time is 10% x 10% = 1% (therefore, the up-time of two cheap systems has increased from 90% to 99%). Adding a third computer will increase that to 99.9%... and each new system added to the cluster will add another "9" to that figure. Which means that we only need six cheap computers for the same 99.9999% reliability.

Ok, this doesn't take into account the infrastructure needed when you have a cluster as opposed to a single system. Let's say that we want to be absolutely sure and use ten cheap computers for reliability, and that the cost of the additional infrastructure is the same as that of the computers. Which means that each individual computer needs to be cheaper than $5,000 in order for solution B to be more profitable than A. (And let's not forget that the expensive system needs a lot of infrastructure too - to guarantee 99.9999% up-time you need controlled cooling, a back-up generator and so on.)

$5,000 for a system that fails one day out of ten? The worst junk I ever used was a lot better than that, and I never paid that much for a system. I think $3,000 was the highest.

The moral of the story: do not underestimate the power of many cheap systems. Google is the best example :)

Thursday, June 26, 2008

I used to hate the site because it doesn't have a "save the whole story" button. Which is annoying, because I want to use my ebookwise reader to read the whole book while sitting in bed, not on a chair looking at the screen.

So... at some point I got pissed bad enough that I felt like writing an application to do it. Since I don't actually have any readers for this blog :P I won't bother with documentation or anything. I've included both the source files and the executable in the archive here. (Yes, I know, I should get a domain, it's not that expensive. I'll do that if I ever decide to actually keep blogging.)

[Edit] Of course, that link is no longer valid. I'll write an article about writing that app at some point.


I watched Zeitgeist the movie yesterday. Nice movie, I recommended it to a few friends for the 2nd and 3rd parts :) (Nice = it fits in with my previous beliefs, of course :P)

As for the first one... well, the whole Horus/Jesus thing is a hoax. This thread is a request by someone for actual evidence... now, the thread is huge, reading it all will take more time than seeing the movie :D, but the thing is, even Acharya's attempt at one point to substantiate her claims fails flat on its face. The conclusion - even by some atheists commenting on the thread - is that the whole thing is Massey's invention, which Acharya parrots because it sells books. (Who knew Dan Brown will practically spawn a new industry, huh?)

So... still a nice movie, and I did learn a lot even from the first part. (Yes, I have long known that the Catholic Church took over a lot of pagan holidays to make Christianity easier to swallow.)

Wednesday, June 25, 2008

A few helper methods in C#

Just a few methods I find myself using in a lot of projects...

  1. public static int ConvertToInt(this string value, int def)  
  2. {  
  3.   int result;  
  4.   return Int32.TryParse(value, out result) ? result : def;  
  5. }  
  7. public static int ConvertToInt(this string value)  
  8. {  
  9.   return ConvertToInt(value, -1);  
  10. }  


  1. string s = "123";  
  2. Console.WriteLine(s.ConvertToInt());  

  1. public static void ForEach<T>(this IEnumerable<T> set, Action<T> action)  
  2. {  
  3.   Debug.Assert(set != null"set");  
  4.   Debug.Assert(action != null"action");  
  6.   foreach (var item in set)  
  7.     action(item);  
  8. }  
  10. public static void ForEach(this IEnumerable set, Action<object> action)  
  11. {  
  12.   Debug.Assert(set != null"set");  
  13.   Debug.Assert(action != null"action");  
  15.   foreach (var item in set)  
  16.     action(item);  
  17. }  

I find it much more expressive to use this ForEach extension method instead of a foreach loop:

  1. list.ForEach(item => { ... do something with item ... });  

rather than

  1. foreach(var item in list)  
  2.   { ... do something with item ... }  

It's probably just a quirk of mine, but I prefer it.

  1. public static string AsXML(this object obj)  
  2. {  
  3.   if (obj == null)  
  4.     return "<NULL />";  
  6.   string str;  
  7.   Encoding utf8 = new UTF8Encoding();  
  8.   var objType = obj.GetType();  
  9.   var xmlatt = (XmlTypeAttribute) Attribute.GetCustomAttribute(objType, typeof (XmlTypeAttribute));  
  11.   using (var ms = new MemoryStream())  
  12.   using (var x = new XmlTextWriter(ms, utf8) {Formatting = Formatting.Indented})  
  13.   {  
  14.     var xs = xmlatt == null ? new XmlSerializer(objType) : new XmlSerializer(objType, xmlatt.Namespace);  
  15.     xs.Serialize(x, obj);  
  16.     str = utf8.GetString(ms.GetBuffer(), 0, (int) ms.Length);  
  17.   }  
  19.   return str;  
  20. }  
  22. public static object AsObject(this string xml, Type type)  
  23. {  
  24.   using (var sr = new StringReader(xml))  
  25.     return new XmlSerializer(type).Deserialize(sr);  
  26. }  
  28. public static T As<T>(this string xml) where T : class  
  29. {  
  30.   return xml.AsObject(typeof (T)) as T;  
  31. }  

Good when you need to (de)serialize pesky objects. Usage:

  1. MyClass x = new MyClass();  
  2. string s = x.AsXML();  
  3. x = s.As<MyClass>();