F# Active Patterns as function parameters

When I first learned about active patterns in F# I thought they were pretty great.  Then I saw you this F# video and learned you can patterns as function parameters.  This lets you do stuff like the following. (note I stole the example from this F# video)

open System.Drawing

let (|RGB|) (color : Color) = (color.R, color.G, color.B)

let printRGB (RGB(r, g, b)) =
    printfn "%d, %d, %d" r g b

printRGB Color.Red

I find this especially cool because I often have a wrapper function to do this sort of data transformation and this is far more elegant.

WPF Transparent Window With Resizing

When I was playing with the FlicScannerWedge Clone I wrote I decided I wanted to make the window look like a barcode cause that would be cool looking.  Plus hey its WPF so it should be able to do something like this. So I found this cool looking image to use as the window background.

Barcode

So I set it as the background and make the window style none and allow transparency and I get the following

image

Bamm!! I was pretty excite that it was so easy but then I went to resize the window and no joy :( .  So I ask Google how to handle this and find Kirupa Chinnathambi’s blog where he shows you how to do this.  The way his example works is by creating a bunch of transparent ‘handles’ around they boarder of your window and then handling some mouse events to do the resizing.  Here’s how my window looks with the handles colored in.

image

So I go through the steps and it resizing and am happy.  Since Kipupa’s was presented more as a proof of concept I pulled out the resizing logic into a base class and did a little refactoring.  The refactoring I liked the most was using Enum,Parse and a dictionary  to replace a switch statement. Turning this

private void Resize(object sender, MouseEventArgs e)
{
    Rectangle clickedRectangle = sender as Rectangle;
    ResizeDirection direction = ResizeDirection.Top;

    switch (clickedRectangle.Name)
    {
        case "top":
            this.Cursor = Cursors.SizeNS;
            direction = ResizeDirection.Top;
            break;
        case "bottom":
            this.Cursor = Cursors.SizeNS;
            direction = ResizeDirection.Bottom;
            break;
        case "left":
            this.Cursor = Cursors.SizeWE;
            direction = ResizeDirection.Left;
            break;
        case "right":
            this.Cursor = Cursors.SizeWE;
            direction = ResizeDirection.Right;
            break;
        case "topLeft":
            this.Cursor = Cursors.SizeNWSE;
            direction = ResizeDirection.TopLeft;
            break;
        case "topRight":
            this.Cursor = Cursors.SizeNESW;
            direction = ResizeDirection.TopRight;
            break;
        case "bottomLeft":
            this.Cursor = Cursors.SizeNESW;
            direction = ResizeDirection.BottomLeft;
            break;
        case "bottomRight":
            this.Cursor = Cursors.SizeNWSE;
            direction = ResizeDirection.BottomRight;
            break;
        default:
            break;
    }

    if (e.LeftButton == MouseButtonState.Pressed)
        ResizeWindow(direction);
}

in to this

private Dictionary<ResizeDirection, Cursor> cursors = new Dictionary<ResizeDirection, Cursor>
{
    {ResizeDirection.Top, Cursors.SizeNS},
    {ResizeDirection.Bottom, Cursors.SizeNS},
    {ResizeDirection.Left, Cursors.SizeWE},
    {ResizeDirection.Right, Cursors.SizeWE},
    {ResizeDirection.TopLeft, Cursors.SizeNWSE},
    {ResizeDirection.BottomRight, Cursors.SizeNWSE},
    {ResizeDirection.TopRight, Cursors.SizeNESW},
    {ResizeDirection.BottomLeft, Cursors.SizeNESW}
};

private static ResizeDirection GetDirectionFromName(string name)
{
    //Hack - Assumes the drag handels are all named *DragHandle
    string enumName = name.Replace("DragHandle", "");
    return (ResizeDirection)Enum.Parse(typeof(ResizeDirection), enumName);
}

protected void ResizeIfPressed(object sender, MouseEventArgs e)
{
    FrameworkElement element = sender as FrameworkElement;
    ResizeDirection direction = GetDirectionFromName(element.Name);

    this.Cursor = cursors[direction];

    if (e.LeftButton == MouseButtonState.Pressed)
        ResizeWindow(direction);
}

Which is a little smaller and more readable IMHO – although probably slower. Here is the source.

Flic Scanner Wedge Source Code

Umm … Lazy

I currently remote desktoping into a computer approximately 2 feet in front of me so I don’t have to lean to the left to grab the keyboard.   Its little moments like this when you remember why you love technology :)

Custom LIRC remote config doesn’t work in Mythbuntu 9.04

I just reinstalled my mythbox and had some trouble with getting LRIC to work.  It turns out that in the new Mythbuntu in the lircd.conf file you need to have quotes around the path to the remote file you want to include.

i.e. you want to have

include “/usr/share/lirc/remotes/one-for-all/lircd.conf.6131n”

NOT!

include /usr/share/lirc/remotes/one-for-all/lircd.conf.6131n

Hopefully this spares some one some grief.

Verizon Mandatory Data

First let me give you a little back story.  My mother has a Motorola Q from Verizon she got about two years ago. image She doesn’t surf the web or anything so she got the pay as you go data plan.   She just uses it for a phone and to run a small booklist app I wrote for her.  This worked out pretty well for her as now she only has the one device to carry around, where as she used to forget to bring the PDA on book shopping trips.  So everything is good and my mom is real happy with the experience, so far so good.

Fastforward two years to the present time and its now time to get a new phone.  As the family geek I start looking into what would be the best new smartphone to get. So I start looking into the latest and greatest from Verizon and find that they have instituted a new policy requiring all smart phones introduced after November first 2009 to have either a $29.99 or $45.99 per month data plan!  This would almost double my mother’s phone bill with no visible benefit to her.  Although I respect the fact the Verizon has the right to do what every it wants with it pricing policy this really sucks for my mother and effectively means she’ll never be able to upgrade to a new smartphone again as long as she stays on Verizon.

So I call up Verizon customer support to try to get full details on the new policy.(Props to Verizon here the support guy was really good and understood the issue straight away and answered all my questions)  He confirmed that there are only three newer phones that can still uses the pay as you go plan, two new versions of the Q and a Samsung model.  And that my mother could buy one of those and add it no problems so she’ll be able to upgrade one last time this year.  Although Verizon doesn’t sell them any more as far as I could tell making here $100 NE2 bonus worthless to her.

Asking why Verizon was implementing this new policy he gave me the company line that the new phones were designed so they will download information for the internet for weather and such automatically and so the new policy was designed to protect the customer from overage charges.  Now I don’t really believe this is true but assuming it is why couldn’t they have just include an option to block all data access instead of requiring a data plan?  This is an option they have for all there phones so its not a technical limitation.

Here are the reasons I think Verizon is implementing this policy and alternative solutions I think they could implement that I think better respect the customers ability to choose.

  1. Verizon doesn’t want to give people cheap smartphones if it isn’t going to make up the money with the data plan
    1. Only give the smartphone discount if you buy a data plan and stay on for a given length of time.  My mother would be willing to spend the $400-$500 for a smartphone if she had to.  Better that than spend $360 a year for a service she doesn’t need.
  2. Verizon apparently had a lot of issues with people choosing the pay as you go plan and then being surprised with the huge charges they got.
    1. Verizon could give customers one month of a free data plan if they sign up with the pay as you go option  and in the bill show them what it would have cost with pay as you go.  If the customer doesn’t switch they are responsible for whatever charges happen no if and or butts.  Obviously this would cost some money on Verizon’s part for the ‘free’ month but they seem to run these sorts of ploys to try to rope you into other services such as their TV service so I don’t think it would be a huge issue for them.
    2. Verizon could require that you either have a data plan or you get data blocked.  Ergo no surprises with pay as you go charges.
  3. Verizon apparently had a lot of issues with people returning smartphones after they bought them without data plans as they didn’t realize all the neat features required data access or what not.
    1. Verizon could charge a additional restocking fee for a smartphone if you either cancel the contract or ‘downgrade’ to a regular phone.  That should ameliorate any costs associated with returned smartphones.

Although my mom has always been happy with Verizon’s service and coverage this new policy has her strongly considering moving to another carrier.  And this is despite all in network benefits she gets since the rest of the family is also with Verizon.  I hope Verizon will retract this policy and replace it with something more customer friendly.