May 2006 - Posts

PowerCollections.MultiDictionary

¿Quieres leer esto en español? Ven acá.

My friend Harley came by with an interesting problem: he had a list of Person objects with properties like id, first name, last name, etc. Now Harley needed to search for people in the list by lastname, what comes immediately to your mind is to use a people dictionary Dictionary<string, Person> and then to search the dictionary with something like peopleByLastName["Smith"], the problem is that .NET Dictionary doesn't accept duplicate keys and of course there are a lot of Smiths ¿what to do? Harley wanted to use a DataSet, populate a DataTable with the list, and then use the DataTable's filtering facilities. It works, but it's a little like killing a bird with a missile. It so happens that the nice guys at Wintellect offer PowerCollections a family of collections that complement those available in .NET, therein they offer MultiDictionary which is a dictionary that accepts duplicate keys, just what Harley was needing. To create a multiDictionary keyed by lastname with the objects of a list you can do something like this:

private MultiDictionary<string, Person> GetPeopleByLastName(IEnumerable<Person> people)

{

  MultiDictionary<string, Person> peopleByLastName = new MultiDictionary<string, Person>(true);

  foreach (Person person in people)

  {

    peopleByLastName.Add(person.LastName, person);

  }

  return peopleByLastName;

}

The true argument in the constructor is the one that allows us to have duplicate keys so that we can enter several people with the same lastname in the dictionary. Now to fetch all the people with a given lastname it's enough to write:

  ICollection<Person> peopleWithGivenLastName = myPeopleByLastName[lastName];

Note that the lastName index returns a collection which can have zero, one or many objects ¡and that's it! Now Harley can search by lastname with duplicates and multiple results. Even better, PowerCollections is free (beer and speech wise), although you do have to register at the site before being able to download it.

Functional programming with Haskell

At the Lima (Perú, not Ohio) Developer Days somebody told me "Ah, you're the funny languages guy". I had to smile and explain that aspect of my life: at college I had a teacher who had a penchant for LISP, being young thus impressionable I acquired the taste for lists and functional programming. Years later I discovered Haskell and when I have a little spare time *and can't be outdoors* I like to play with this nice little language. A small sample:

Before explaining the qsort function, let's discuss some of its uses (and the use of lists in Haskell):

  1. [2,500,-1,8,6,2000] is a six integers list (duh)
  2. Note that qsort works also with strings, by the way it's not that Haskell hasn't got strong typing but rather that it's got just-in-time type inference
  3. reverse is a predefined function that, well, reverses a list (in this case the list that results from applying qsort). Parenthesis are necessary as (reverse qsort) is not possible (reverse applies to lists not to functions, but note that Haskell can actually apply functions to functions) 
  4. [49,47..1] expands to the list of all odd numbers between 49 and 1, note how Haskell infers the sequence. ++ is the list concatenation operator.

Now, let's dissect the qsort function:

  1. qsort [] = [], ordering an empty list gives... an empty list
  2. qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs), ordering a list that start with x followed by xs (the list of all items behind x) is the same as:
    1. Taking all elements lower than x ( filter (< x) xs ) and ordering them (using qsort itself of course)
    2. Putting [x], that is the list with x as its sole element, in the middle
    3. Taking all elements greater or equal to x ( filter (>= x) xs  ) and qsorting them

Compact and easy to understand (once you are comfortable with the syntax). Now that is in vogue to explore alternative languages like Python or Ruby, giving a look to Haskell or his functional relatives wouldn't be a bad idea. You will find a short and engaging introduction to Haskell here. Alas, for my examples I use WinHugs, a Haskell interpreter that runs on Windows, very handy for experimenting with the language (the latest version, May 2006, is fresh from the oven).

Windows Forms 2.0 Libraries

I try not to do advertisments on commercial products, but there are a few tools or libraries that, IMHO, really deserve it. One of them is Janus Systems WinForms Control Suite. If you need to create user interfaces that really show off, I suggest you to give Janus a try.

I know many people use Infragistics with good success but, IMHO again, Infragistics is bigger and more complex than necessary, and when talking about tools and libraries I try to go the focused and light way. By the way, which WinForms libraries have you used in your projects?

Disclaimer: I have no association with Janus Systems (Hint: not even a free review copy [;-)]).

Posted by Edgar Sánchez with no comments
Filed under: ,

Happy Birthday Informatics!

In 1936 Europe was on an inevitable path to World War II, Ecuador declared the Galapagos Islands a national park. In England, a 24 years old mathematician proposed in his whitepaper On computable numbers, with an application to the Entscheidungsproblem a solution to the problem of whether there is an algorithm to determine if any given logic statement is valid or not. The interesting thing for us is the path that Alan Turing took to attack the problem: define a symbol processing machine that takes a symbol string input, decides on every symbol, and writes a symbol string output.

Yeah, that's right: input, processing, and output. The Turing Machine is a formalization of our concept of digital computer (actually, any modern day computer can be described in terms of a Turing machine), and with his work Turing defined not only the possibilities but the computing limitations of such a device. For those interested in how a Turing machine works, you can download a simulator written in C#.

The aforementioned whitepaper was proposed by Turing on May 28th, 1936, and I think this is a sound date to be considered as the formal birth of Informatics. At its seventies, ours is an ultra-young engineering with plenty of life to live ahead so ¡Happy Birthday Informatics!

Ah, by the way, whatever happened to the Entscheidungsproblem? Turing proved that it wasn't possible to create an algorithm that could decide whether a theorem was valid or not, in other words: math is harder than programming Stick out tongue [:P].


More Posts