Tech Tock

Time is of the essence.

Functional Homework

MejerTShirtAs I mentioned last month, I want to learn a functional programming language.  Lucky for me, Dr. Erik Meijer (and his electric t-shirt) gave a course on Haskell in October and its posted on Channel 9.

I watched the first lecture over a week ago but haven’t had time to do the homework till now.

Here it is in C#.  Its quicksort written as he described it in Haskell.  It doesn’t feel that different from the programming I usually do, but it is recursive (which I generally avoid), so maybe that’s something.  At least I did my homework, now I can go on to the second lecture :).

As long as I was making a program, I figured I’d expand my WPF horizons and used an attached behavior for the button.  Of course its MVVM which is just a habit now.

Attached Behaviors

It wasn’t obvious how to link the static attached behavior to the instance properly.

A guy on StackOverflow recommended getting the DataContext from the sender in the event:

private static void ButtonOnClick(object sender, RoutedEventArgs args)
{

var fxElem = sender as FrameworkElement;

var vm = (VM) ((FrameworkElement)args.OriginalSource).DataContext;

Most other explanations of attached behaviors are limited to working with the control itself and not the ViewModel:

http://eladm.wordpress.com/2009/04/02/attached-behavior/
http://www.codeproject.com/KB/WPF/AttachedBehaviors.aspx

You can download the code here.

image

Here’s the homework code:

public  IEnumerable<IComparable>
    Sort(IEnumerable<IComparable> listToSort)
{
    if (listToSort == null)
        throw new InvalidOperationException
            ("Cannot sort a null list.");
    if (listToSort.Count() == 0)
        return listToSort;
    var result = new List<IComparable>();
    var start = listToSort.First();
    //smaller on left
    result.AddRange(Sort(listToSort.Skip(1)
        .Where(e => e.CompareTo(start) <= 0)));
    //val in middle
    result.Add(start);
    //larger on right
    result.AddRange(Sort(listToSort.Skip(1)
        .Where(e => e.CompareTo(start) > 0)));
    return result;
}

Here’s some highlights of the attached behavior class:

public static class SortStarter

{

// // RegisterAttached for attached behaviors

public static readonly DependencyProperty SortBehaviorProperty =

DependencyProperty.RegisterAttached(“SortBehavior”, typeof(bool), typeof(SortStarter), new UIPropertyMetadata(false, SortBehaviorChanged));

// Get/Set Methods for the attached behaviors:

public static bool GetSortBehavior(DependencyObject obj){

return (bool)obj.GetValue(SortBehaviorProperty);}

public static void SetSortBehavior(DependencyObject obj, bool value){

obj.SetValue(SortBehaviorProperty, value);}

// Method to hook the event

private static void SortBehaviorChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs args){

var button = dpo as ButtonBase;

if (button != null){

if ((bool) args.NewValue)

button.Click += ButtonOnClick;

else

button.Click -= ButtonOnClick;

}

// Button Click Handler:

private static void ButtonOnClick(object sender, RoutedEventArgs args){

// Don’t process calls from up the visual tree

if (!Object.ReferenceEquals(sender, args.OriginalSource))  return;

//get VM from DC

var fxElem = sender as FrameworkElement;

var vm = ((FrameworkElement) args.OriginalSource).DataContext as VM;

// process with VM and new Sorter class

}

Hooking up the button in XAML is simple:

<Window x:Class=”WpfApplication1.Window1″

xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”

xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”

xmlns:local=”clr-namespace:WpfApplication1″

Title=”Window1″ Height=”129″ Width=”300″>

<Button local:SortStarter.SortBehavior=”True” >Sort</Button>

Advertisements

December 26, 2009 Posted by | Uncategorized | , , , | 1 Comment

Considering Adding a Functional Language to My Bag of Tricks

There are a lot of people @Lab who know things like Erlang, Haskell, Python, R, etc., etc., etc.  I’m actually considering learning Lisp, but I need a project.  I “learned” php for a project over the summer, but I didn’t really like it since its so much like classic ASP (really messy).  Maybe I’ll learn F# instead…  That shouldn’t be such a strech since I built an ML compiler in grad school (F# is based on ML).  I wonder what kind of project I’d be interested in would benefit from a functional language…  I suppose I’ll have to read up on the language and maybe that will give me some ideas.

Its funny, doing WPF, my umpteenth technology, is just not enough.

November 13, 2009 Posted by | Uncategorized | , , , , , | 2 Comments