Two days left until CodeMash

Sing to the tune of “Christmas is coming”

CodeMash is coming
The talks are getting full,
Please check the schedule
For the things that rule.

If you haven’t got the F# skills,
The Scala skills will do,
If you haven’t got the Scala skills,
The God bless you.

OK, that was lame, but I don’t care. I’m really looking forward to the excellent talks at CodeMash. Yes there will be many open spaces, and they will be splendid. The majority of CodeMash is still classic eyes forward sessions. Nearly all of them that I attended last year were brilliant.

I still haven’t picked which talks I am going to see. What talks are you planning to see?

Multimap is Lookup

Breaking the tradition of not giving developers even the most basic of data structures with which to work, Microsoft, in its magnanimous wisdom, chose to include a multimap implementation in the 3.5 version of .NET. This was the same time that it first gave developers a Set implementation too. Its amazing any developers tolerated not having these out of the box with .NET over the years, but I guess making them yourself is the norm for us old C/C++ types.

(I shouldn’t give MSFT such a hard time. Java doesn’t ship a multimap either, you are left to use Apache Commons or Google Collection Library. http://java.sun.com/docs/books/tutorial/collections/interfaces/map.html)

Not wanting to break all traditions, they screwed up on the naming. Calling it a multimap would have been too easy. After all, we don’t call our resizable arrays “vectors” do we? No, we call them Lists. But aren’t Linked Lists something else, well, yes they are, Lists aren’t Linked Lists, and Linked Lists aren’t Lists, at least not in the IList<> of the sense. Enough Ranting.

(http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html v. http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx)

In the System.Core assembly in the System.Linq namespace there is an Interface named ILookup`2. There is an implementation of it called Lookup`2, but you can’t construct it. Yes, as if poor naming weren’t enough, It seems that MSFT doesn’t actually want you to use a multimap. Taking a look at return types and interface definitions, it seems this type exists only to support the Groupby LINQ implementation. Being the lazy programmer that I am, I do not want to write a multimap. The existence of the ToLookup extension method on IEnumerable, tells me that maybe I can get at one of these things.

In this example, assume I have a grocery shopping list which I’ve named mmap so that you remember it is a multimap. I want to organize (key) things in my list according to where I will store it, the fridge or the freezer.

var mmap = (
    new [] {
        new {Key = “Freezer”, Item = “IceCream” },
        new {Key= “Freezer”, Item = “Blueberries”},
        new {Key= “Freezer”, Item=”FCOJ”},
        new {Key = “Fridge”, Item=”Milk”},
        new {Key=”Fridge”, Item=”Yogurt”},
        new {Key = “Fridge”, Item = “Eggs”}
    }
    ).ToLookup(a=>a.Key, b=>b.Item);
foreach(var map in mmap)
{
    Console.WriteLine(map.Key);
    foreach(var val in map)
    {
        Console.WriteLine(“\t”+val);
    }
}

//look’em up by index
Console.WriteLine(mmap[“Freezer”]);
//but that is just an IGrouping<>
foreach(var item in mmap[“Freezer”])
{ Console.Write(item);Console.Write(“\t”); }
Console.WriteLine();

Some things to note:

  • We are exploiting anonymous types, they are very useful here.
  • There is no reason that Key and Item have to be named as such, I just found this most readable.
  • There is no reason Key and Item have to be typed as String. They could be any type, but the key type should support equality comparison for the grouping to function.
  • ToLookup() returns an ILookup`2 and not a Lookup`2.
  • The resulting multimap is immutable, like most things LINQ.

An expert LINQer might say “this is no different than group by”, and at first glance, I’d agree. There is an important difference. Under the hood it is likely that this is implemented exactly the same as group by, but ToLookup returns an ILookup`2. ILookup`2 extends IEnumerable[IGrouping`2]. The latter is what Groupby returns. This is important because the ILookup`2 interface exposes the indexer so that you can get at your map by indexing by key.

I wish I could just new up a Lookup and add items rather than jumping through hoops (see a future post). But I’m happy to never write another multimap implementation again.

Three days left until CodeMash

I’m not just hoping, but I’m expecting to have my mind blown.

Sometimes even open spaces is too structured. The structure is good, but people feel a need to stay on topic. I appreciate this, but I also like the completely unstructured total geek talk that you can get in the hallway. Michael Letterle and Joe O’Brien blew my mind with some conversation last year. I’m sure it will be something else this year, and I can’t wait.

For what are you hoping and expecting at CodeMash? Anything mind blowing?

Four days left until CodeMash

I’m excited to so something weird at CodeMash:

Adding things to the Open Spaces board that I know nothing about! What better way to learn about something and meet people who know those things at the same time. Maybe there will be someone (other than Dan) who knows about writing Brew applications and can tell me that I’m crazy for even thinking about it.

What are you excited to do at CodeMash?

Functional Functions

Jared Parsons wrote a post very similar to one that I have been wanting to do for a while. Check it out: http://blogs.msdn.com/jaredpar/archive/2008/12/02/mapping-linq-to-f.aspx

His post maps the common C#/VB.NET LINQ methods to F# function names.

I like to think about this a little differently. There are a bunch of well known functions in programming which tend to get used a bit more in functional programming. C#/VB.NET and the BCL names things rather unintuitive if you are accustomed to the functional names for these functions.

The basics:

map Select
reduce Aggregate
filter Where

To throw a little more confusion in the mix, there are aliases and special cases for reduce.

Fold is an implementation of reduce which implies some order to how the reduction is performed. Usually fold is an alias for fold right, which means go from left to right. The other is fold_right which does the same thing but goes from right to left.

Sum, Max, Min and Average are all special cases of Aggregate or reduce.

If you are new to functional programming it will help to remember all of this. Read the wikipedia page for map: http://en.wikipedia.org/wiki/Map_(higher-order_function) and be sure to look in the See also section for the reduce page and the external links for this link: http://www.cse.unsw.edu.au/~en1000/haskell/hof.html

— begin joke here —

F# does an interesting thing and names its generic generator function “unfold” since it is the opposite of fold. This means it is the opposite of reduce. Since any specific functions implemented with fold can be considered a reduction function, and in chemistry reduction has an opposite, I like to call the concept of unfold by the name “oxidate” or “oxidize”. e.g. here is an oxidation function for Fibinacci sequence until the value is one million. It is lifted from Chris Smith’s blog:

let fibs =

        (1,1) |> Seq.unfold

            (fun (n0,n1) ->

                if n0 <= 1000000 then

                    Some (n0, (n1, n0 + n1))

                else

                    None)

Finally, there is one more similar terminology: generators.

Python calls methods like this “generators” although the docs in PEP 255 also calls them “producer functions”. PEP 289 defines a short hand expression for these things which is very similar to F# sequence expressions.

Now that IronPython is 2.0 final and we know F# will be shipped with Visual Studio 2010, please remember that sequence expressions in F# and generator expressions in python are just two forms of oxidation.

Windows Home Server is cool, but confusing

I want to test file restore, so my first instinct is, use Windows Explorer.

After hunting around, the only thing I can find is the Backup Now button in the properties for the C drive.

Very strange that “Backup Now” doesn’t start a backup wizard, but instead displays the Backup Status and Configuration screen which includes restore functions.

BackupNow_2008-12-04_21-00-37

Weird, but it does have for what I was looking… Restore.

OH no!

nobackups_2008-11-30_11-44-34

It says “There are no backups available on this computer.”

That is scary to me as a user. Fortunately I realize that MS just doesn’t use their vista backup system to backup with Windows Home Server. I can’t imagine why not. Is it not good enough?

So far, the only way I have found to restore files with Home Server is to actually login to the Home Server Console. Good enough, I guess.

Opera, Chrome, Konqueror, Safari and other Non Firefox and Internet Explorer browsers

Should be considered just as important as Firefox and Internet Explorer.

Unfortunately, not even Chrome is given 1st class status as a browser by Google:

googleOperaFail_2008-12-02_10-55-39

in case you cannot read the image it says “Page editing not supported in your web browser. Download a new copy of Firefox or Internet Explorer to edit pages.”

Its 2008 Google, the browser wars used a Terminator style time travel device to show up on my doorstep in a sphere of lightning.