All posts by jrwren

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?

Six days left until CodeMash

Something I want to get out of CodeMash:

I want to ask F# experts more about Workflows. I think I grok Async Workflows, but workflows in general are still not a tool which I feel I have mastered. When would I use one? Why would I use one? What is the point again? I always forget.

What do you want to get out of 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.

Live Mail Beta is a bummer software fail

Using beta software is sometimes a bummer

windowslivemailfail_2008-12-02_22-49-10

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

WindowsLiveMailBeta_2008-10-28_09-52-42

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.

operaPopUp_2008-10-30_20-16-54

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.

operaCustom_2008-10-30_20-17-35

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.

firefoxBookMarkBar_2008-10-30_20-18-48

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: http://medsphere.org/projects/gtksharp/releases/2.10.4/gtksharp-sdk-2.10.4.msi
    The project page is here:
    http://medsphere.org/projects/gtksharp/
    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.
    newGTK#_2008-10-30_19-54-28
    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.
    projectTarget_2008-10-30_20-00-11
  6. Write some test code
  7. [STAThread]
    static void Main()
    {
        Application.Init();
        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!!!!";
        myWin.Add(myLabel);
        myWin.ShowAll();
        Application.Run();
    }
    static void myWin_Destroyed(object sender, EventArgs e)
    {
        Application.Quit();
    }
  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 = 
new
{
    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" } }
}.ToDictionaryOfDictionariesOfStrings();

Assert.IsTrue(things.ContainsKey("Humans"));
Assert.IsTrue(things.ContainsKey("Dogs"));
Assert.IsTrue(things.ContainsKey("Marsians"));
var humans = things["Humans"];
Assert.IsTrue(humans.ContainsKey("Johny"));
Assert.IsTrue(humans.ContainsKey("Gina"));
var gina = humans["Gina"];
Assert.IsTrue(gina.Contains("Diner"));
Assert.IsTrue(gina.Contains("Pay"));
var dogs = things["Dogs"];
Assert.IsTrue(dogs.ContainsKey("Sparky"));
Assert.IsTrue(dogs.ContainsKey("Toto"));
var sparky = dogs["Sparky"];
Assert.IsTrue(sparky.Contains("beer"));
Assert.IsTrue(sparky.Contains("baseball"));

Good times.