All posts by jrwren

Functional Functions

Jared Parsons wrote a post very similar to one that I have been wanting to do for a while. Check it out:

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: and be sure to look in the See also section for the reduce page and the external links for this link:

– 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))



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.


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

OH no!


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:


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.

Live Mail Beta is a bummer software fail

Using beta software is sometimes a bummer


That one is at least a nice descriptive message. Actually, after killing the wlmail.exe process it fires right back up.


This one is a bigger bummer that I ran across a while ago, but after finding the Contacts folder burried in AppData some place, and renaming it, the program at least ran. I am disappointed to have lost my contacts.

Other than those two glitches, Windows Live Mail is a very nice and FAST (I’ve been dragging along with Thunderbird) email client.

I Use Opera

People make fun of me, but I don’t care. I use Opera. Its faster. I use javascript heavy sites like google mail and bloglines and in Firefox and Internet Explorer these sites are slow to load, slow to use, and make my browsers eat upward of 30% of the 2.5G of ram in my poor laptop.

I know the google lovers say “CHROME!” but after reading the privacy policy, I can’t handle it. Opera is years more refined and has the options I need. I do miss noscript, my favorite firefox plugin, but with the ability to enable or disable Java, JavaScript, plugins, cookies, sound, animated images, and even refers, just by pressing F12 and selecting one of these options, I’m fine with using Opera. Did I mention it is fast? I also love the saved session state.


Two things I missed when I moved from Firefox to Opera were the smart bookmarks which I had configured in Firefox to post to my delicious account and to subscribe to a feed using bloglines. It turns out Opera has custom buttons.

After finding the Del.Icio.Us custom buttons, I was able to make my own for bloglines.


s/bl <—drag this link to your Opera menu bar

Just drag this link into your menu. I like to name my s/bl for subscribe with bloglines. I like tiny abbreviations so that my menu doesn’t fill up.


GTK# in Visual Studio 2008 on Vista x64

Yes, I am crazy. Why would any programmer want this combination? I think it is a combination of wanting to work with the best tools, in this case Visual Studio 2008 and CodeRush and wanting to target Win32, OSX, and Linux all at once. The very nice part is that thanks to the hard work of other people, you don’t even have to run in Mono on Win32.

  1. Install the GTK# SDK
    You can get it from here:
    The project page is here:
    Thanks to the good folks at medsphere for maintaining this windows installer. Presumably they use this in their applications.
  2. Create a new Windows Forms Project in Visual Studio.
    Its fine to keep 3.5 framework selected. Mono supports the core parts of 3.5.
  3. Remove the references to System.Windows.Forms.dll
  4. Add reference to atk-sharp, glib-sharp and gtk-sharp in the following paths:
    C:\Program Files (x86)\Medsphere\Gtk# Runtime\lib\gtk-sharp-2.0\atk\atk-sharp.dll
    C:\Program Files (x86)\Medsphere\Gtk# Runtime\lib\gtk-sharp-2.0\glib\glib-sharp.dll
    C:\Program Files (x86)\Medsphere\Gtk# Runtime\lib\gtk-sharp-2.0\gtk\gtk-sharp.dll
  5. Change your Platform target to x86 from AnyCPU in the project properties.
  6. Write some test code
  7. [STAThread]
    static void Main()
        Window myWin = new Window("My first GTK# Application! ");
        myWin.Resize(200, 200);
        myWin.Destroyed += new EventHandler(myWin_Destroyed);
        Label myLabel = new Label();
        myLabel.Text = "Hello World!!!!";
    static void myWin_Destroyed(object sender, EventArgs e)
  8. Run the application

Enjoy your 3rd option for a pure .NET programming GUI Toolkit on Win32. Winforms and WPF are great, but GTK# does fill a certain niche.

Dr. Strange HoH: How I Learned To Love Perl and C#

Its no secret that I came to C# after doing lots of Perl. I treasure my Perl experience. Many claim that Perl is unreadable, but I argue that the language is as good as the programmers using it. I’ve seen pages of equally unreadable PHP, VB, and yes, even C#. I’ve used Perl libraries as references for how things work. Many Perl libraries are examples of great highly readable code. Language doesn’t matter.

Perl’s documentation is a testament to open source languages. I learned Perl with perldoc installed an the perl man page. It taught me the Perl way of data structures and as I got more advanced I ran into Arrays of Arrays(AoA), Arrays of Hashes(AoH), Hashes of Arrays(HoA) and Hash of Hashes(HoH). These are all defined in the perllol perldoc.

Some of the most confusing Perl that I ever did read or write was confusing because instead of building explicitly named types, convention was relied on too much over configuration and everything was just a HoHoHoHoHoHoH. I think it was a joke about Santa Clause.

All that said, there are times when you really want a HoH or a HoHoH or a HoHoHoH. That is Dictionary<string,Dictionary<string,string>> or Dictionary<string,Dictionary<string,Dictionary<string,string>>> or Dictionary<string,Dictionary<string,Dictionary<string,Dictionary<string,string>>>> for you non perl types.

A little over a year ago, Eilon Lipton wrote a post on using anonymous types as Dictionaries. I rather liked it, but That was just a H (Dictionary<string,string>).  I recently had the need for a HoHoA.

I’m not advocating using this code or ever writing code like this. In fact, I discourage it. If you can find a way to do the same thing but with compile time checks, please do that instead. The problem with using anonymous types as dictionary literals is that you won’t know until runtime that you have made a mistake. WRITE YOUR UNIT TESTS!

public static Dictionary<string, string> ToDictionary(this object source)
    var dict = new Dictionary<string, string>();
    var props = source.GetType().GetProperties();
    Array.ForEach(props, p => dict.Add(p.Name, (string)p.GetValue(source, null)));
    return dict;
public static Dictionary<string, IList<string>> ToDictionaryOfStrings(this object source)
    var dict = new Dictionary<string, IList<string>>();
    var props = source.GetType().GetProperties();
    Array.ForEach(props, p => dict.Add(p.Name, (IList<string>)p.GetValue(source, null)));
    return dict;
public static Dictionary<string, Dictionary<string,string>> ToDictionaryOfDictionaries(this object source)
    var dict = new Dictionary<string, Dictionary<string, string>>();
    var props = source.GetType().GetProperties();
    Array.ForEach(props, p => dict.Add(p.Name, (Dictionary<string,string>)p.GetValue(source,null).ToDictionary()));
    return dict;
public static Dictionary<string, Dictionary<string, IList<string>>> ToDictionaryOfDictionariesOfStrings(this object source)
    var dict = new Dictionary<string, Dictionary<string, IList<string>>>();
    var props = source.GetType().GetProperties();
    Array.ForEach(props, p => dict.Add(p.Name, (Dictionary<string, IList<string>>)p.GetValue(source, null).ToDictionaryOfStrings()));
    return dict;

Yes, you have to call the right method, it isn’t magic. Yes, these extension methods pollute all objects with their clutter. They do work when you need them though!

var things = 
    Humans = new { Johny = new[] { "Guitar", "Docks" }, Gina = new[] { "Diner", "Pay" } },
    Dogs = new { Sparky = new[] { "beer", "baseball" }, Toto = new[] { "witch", "kansas" } },
    Marsians = new { Marvin = new[] { "death ray", "moon laser" }, Quato = new[] { "speech", "power" } }

var humans = things["Humans"];
var gina = humans["Gina"];
var dogs = things["Dogs"];
var sparky = dogs["Sparky"];

Good times.

msysgit installer is broken on vista

OK, it isn’t exactly broken, it just doesn’t know about vista’s virtual path features. This means you can run the exe as a non-admin user and it does not get installed properly.

The work around is to not run the installer as a regular use and wait for UAC to invoke itself. It never will. Instead, save the .exe file and right click on it and choose "Run as administrator…"

Now the program will actually get installed to C:\Program Files or C:\Program Files (x86) instead of the virtual are c:\Programs\data\user\blah\wahterver\it\is\vista\has\some\crazy\features.

Why am I using GIT? Well, unfortunately because I have to. Cloning was easy enough but just typing git help or git help clone and I immediately miss the polish of bazaar. The "I have to" is in reference to the no longer ignorable myriad of projects now hosted at github. IMNSHO all of these should have opted to be hosted a launchpad. But as we have seen in the past in technology, the best solutions don’t always win. Sometimes the solution with the loudest voice wins. (VHS>Beta?) (DAT>DCC?) (Blueray>HDDVD?)

Lack of Value of Code Metrics

I was just reading blogs and stumbled across this gem:

Patrick ran NDepends (an awesome static analysis tool) on the 2.0 release of NHibernate.

Patrick went on to suggest that the NHibernate code base was spaghetti because its internal namespaces are cross dependant. Having looked at the NHibernate code base, I disagree. In fact as a base for learning more about programming by reading others source code, I have found NHibernate to be an excellent source of learning.

I tried to post this as a comment, but I couldn’t (maybe my Opera browser, or lack of JS?).

“IMHO, the NHibernate team should fix this problem asap”

I can fix it easily, just flatten out the namespaces. Put EVERYTHING into the NHibernate namespace and this wouldn’t be a problem.

Is this the right thing to do?  NO!  But it does fix your metric.  So I assert that your metric is wrong.

Who care if namespaces are cross dependent?  I don’t. Namespaces are for logical groupings of behaviors. If your behaviors are cross dependent, so will your namespaces.

This metric may be useful in a typical 3 tiered layered business application. NHibernate is not that. It is one ORM library. ONE. I do not find your metric useful.


Given any metric that you are using (P/E ratio for stocks anyone?) please be aware of what that metric actually means and consider its value in that specific case. Yes, you must reconsider each metric on a case by case basis.

New Server and My Worthless Linux Account Management

I got a new server for my home, more to come on that later. Right now I want to talk about my worthless account management practices. You see, a long time ago (4 years) in a land far far away (2 counties), I was a system administrator. I knew all the best practices. I could plan a server migration with zero downtime or if some DT was required  could minimize it. I could keep my servers up with 3-4 9’s (99.99?%).

I think I’ll compare my situation to that of the super clean janitor who invites you into their home only to reveal that they live in squalor. My home server works, but it is a mess. So it is wholly appropriate that when it came time to replace my aging server with 3-4 years of Linux cruft (initially installed with Breezy Badger and then upgraded) that I throw caution to the wind and just install Linux and see what kind of problems I run into.

Well, the first problem I run into is that my baby cannot watch her Baby Signing Time DVD rips from the server — I know ripping DVDs is a violation of the DMCA, but Jeff Atwood says it is OK, so if you are an MPAA lawyer, please see Jeff, he is a top-10 blogger and must have way more money than I do — the XBMC wouldn’t work. Well DUH!  The only account on the new system is mine. I hadn’t created any XBMC account for the samba share, nor had I set its samba password.

Since I’m sure many other people might reinstall and not upgrade, but would like to not have to reconfigure everything, this is my little migration guide. I’ll assume that you have a backup or your old hard disk lying around and that you are capable of getting at the needed files in some way. In my case, my old root file system is mounted /mnt, so when you see /mnt paths in these commands, just remember that is my old disk.

Jay’s Stupid Guide to Account Migration

First, it is important to keep in mind that Ubuntu, Debian and AFAIK every Linux distribution these days creates necessary system user accounts for you on demand. e.g. I didn’t have a bind account and group on my system until I installed the bind9 package. This means the the uid/username map will most certainly be different between your old system and your new system. I’m OK with this. I want a new system. I just want some old user accounts.

Visual Inspection

$ cat /etc/passwd


jrwren:x:1000:1000:Jay R. Wren,,,:/home/jrwren:/bin/bash




Cool, so there is my account, and there are a few others. One for my wife, one for a great friend, and I left out a few more. But there aren’t too many, so if I had to manually do this, I could do that. If I really had to copy and paste lines in passwd and shadow I could. But I won’t.

Let Them Login

Use your eyes and know your regex. In my case the uids for all of the accounts I wanted moved were 1001-1007, so I confirmed that my regex was correct and then just appended via pipe using tee.

$ grep x:100[1234567] /mnt/etc/passwd | sudo tee -a /etc/passwd

I could now run the “id” command for these new users and it would work!  Neato.

But I’d like them to be able to login with their old password in case they were at some place without their ssh public key, so I set out to do the same thing with shadow. Shadow is keyed by username, not uid, so we just have to map back to that.

$ grep x:100[1234567] /mnt/etc/passwd | awk -F: ‘{print $1}’ | sudo grep -f – /mnt/etc/shadow | sudo tee -a /etc/shadow

Cool, now people can login.

Power In Numbers and Groups

If we look at the stat(3) of a file from the old system as owned by one of these migrated users now, then the uid maps to the correct name but the group is incorrect. We need to do the same thing for groups.

$ grep x:100[1234567] /mnt/etc/passwd | awk -F: ‘{print “x:”$4}’ | grep -f – /mnt/etc/group | sudo tee -a /etc/group

So we map a little differently and we construct our fgrep regex using awk, but its mostly similar to the shadow case.


Regular user accounts are all neat and good, but the whole point was XBMC which connects via samba share. This is where my sysadmin knowledge is starting to show its age. The last time I migrated samba accounts the accounts were in an smbpasswd file and could be moved very much like the shadow case above. These days samba stores this information in a trivial database known as a tdb. (No idea why an html file named msdfs actually contains tdb information instead of DFS information.)

I had to search the docs and google a bit before I found this post:  which lead me to the pdbedit command. Samba has changed since I last used it.

Unfortunately, I didn’t have tdb password entries for all these user accounts, actually I had ‘em only for two. No big deal since the easiest path is to go through the old smbpasswd file format to get here.

First thing, I found that pdbedit doesn’t like read only file systems, so copy your pdb local (did I mention my old disk is mounted read only at /mnt?)

$ cp /mnt/var/lib/samba/passwd.tdb oldpasswd.tdb

Now we need that old smbpasswd file format

$ sudo pdbedit -Lw -b tdbsam:oldpasswd.tdb  > smbpasswd

Then I want to filter it for only the accounts I care about.

$ grep x:100[1234567] /mnt/etc/passwd | awk -F: ‘{print $1}’| grep -f – smbpasswd > smbpasswd.stripped

Then (thanks to abartlet #samba on freenode for guiding me) merge these into the new systems passwd.tdb

$ sudo pdbedit -i smbpasswd:smbpasswd.stripped

Now you should have functioning samba passwords.

If someone reading this is thinking, oh this is WAY easier in windows, could you please link me to the step by step process like this? I’ve always wanted to use it and I could never get it done.