QuickSort in Functional C#

Here's a very simple QuickSort algorithm from Wikipedia written in a very Functional way of C#. Notice that it looks very close to the pseudo-code algorithm shown in Wikipedia. The pivot used is just sequence.First(). You can replace the pivot by some random position if you wish.

   1:          public static IEnumerable<T> QuickSort<T>(this IEnumerable<T> sequence) where T : IComparable<T>
   2:          {
   3:              return !sequence.Any()
   4:                         ? sequence
   5:                         : ((from x in sequence.Skip(1)
   6:                             where x.CompareTo(sequence.First()) < 0
   7:                             select x)
   8:                               .QuickSort()
   9:                           ).Concat(new[] { sequence.First() }).Concat((from x in sequence.Skip(1)
  10:                                                                        where x.CompareTo(sequence.First()) >= 0
  11:                                                                        select x).QuickSort());
  12:          }

I tried making this more fun by using the Fix Point Generator which I mentioned previously, and converted that code into a Func<IEnumerable<T>> instead. Here's the code.

   1:          public static Func<T, T> Fix<T>(Func<Func<T, T>, Func<T, T>> F)
   2:          {
   3:              return x => F(Fix(F))(x);
   4:          }
   5:   
   6:          static void Main()
   7:          {
   8:              var quicksort = Fix<IEnumerable<int>>(
   9:                  qsort =>
  10:                  sequence =>
  11:                  !sequence.Any()
  12:                      ? sequence
  13:                      : qsort(from x in sequence.Skip(1)
  14:                              where x.CompareTo(sequence.First()) < 0
  15:                              select x)
  16:                            .Concat(new[] { sequence.First() })
  17:                            .Concat(qsort(from x in sequence.Skip(1)
  18:                                          where x.CompareTo(sequence.First()) >= 0
  19:                                          select x)));
  20:          }

This is a perfect example of how you can use the Fix Point Generator to create your recursive functions on the fly. If you want to make it reusable with an extension method, you can easily convert it to the extension method, or a normal method. Personally I feel that using the Fix Point Generator is more intuitive and slightly more readable as oppose to the extension method way of doing things.

One point to note is that the "Concat" extension only accepts an IEnumerable<T> and not a single item. You can solve this by the above, "new[] { sequence.First() }" which is a cleaner way if you don't want to write your own extension method (which you could too).

Crazy stuff eh?

Presentation: Practical Functional Programming in C#

Here are my slides I've done at DevTeach Toronto 2008

Download it here.

Getting Bluetooth working on Windows Server 2008

If your bluetooth adapter is a Broadcom Bluetooth 2.0 EDR, like the "ThinkPad Bluetooth with Enhanced Data Rate" or any Broadcom Bluetooth 2.0 EDR type adapter, you can follow the steps below to get bluetooth working on Windows Server 2008.

Required for install:

Vista or WS2008 x86: http://www.toshiba-tro.de/tools/bluetooth/BT-stack.zip
Vista or WS2008 x64: http://www.toshiba-tro.de/tools/bluetooth/BT-stack-64bit.zip

Hex Editor, e.g. WinHex or CodePad
 

1. If you down it, you will install this software. When Toshiba Setup say Plug your BT device, you'll click to cancel. Install is succesfull.

2. Open Device Manager (Start | Control Panel | Device Manager) or Start | Run... | devmgmt.msc

You find in Other devices - Bluetooth dongle. Get Properties of it. On fold Details you see Property, It scroll to Hardware Ids. In Value Box u see two line, e.g.:

USB\VID_0A5C&PID_2101&REV_0100

USB\VID_0A5C&PID_2101

You copy second line with short Ids ('USB\VID_0A5C&PID_2101')

3. Now you going to folder:
Vista or WS2008 x86: %PROGRAMFILES%\Toshiba\Bluetooth Toshiba Stack\Drivers\tosrfusb

Vista or WS2008 x64: %PROGRAMFILES(x86)%\Toshiba\Bluetooth Toshiba Stack\Drivers\tosrfusb

Here are files: tosrfusb.cat, tosrfusb.inf, tosrfusb.sys
You need edited *.cat, and *.inf file.

4. Open tosrfusb.inf in Notepad.exe (e.g. Start | Run... | Notepad.exe)

Here you must edited line 161:

%TosrfUsb.DeviceDesc97%=TosrfUsb_Device,  USB\VID_0C24&PID_000F

and line 288:

%TosrfUsb.DeviceDesc97%=TosrfUsb_Device,  USB\VID_0C24&PID_000F

You edit this lines to:

%TosrfUsb.DeviceDesc97%=TosrfUsb_Device,  USB\VID_0A5C&PID_2101

And save as tosrfusb.inf (replace original file).

5. Open tosrfusb.cat in Hex Editor (e.g. WinHex, CodePad).

Ctrl+F.. You need line of characters (but not find this data as text value!):

4800570049004400390037020410010001042C7500730062005C007600690064005F00300063003200340026007000690064005F0030003000300035

or

H.W.I.D.9.7.......,u.s.b.\.v.i.d._.0.c.2.4.&.p.i.d._.0.0.0.5

or

HWID97   ,usb\vid_0c24&pid_0005

There you must change value help by ASCI table, e.g.:

4800570049004400390037020410010001042C7500730062005C007600690064005F00300041003500430026007000690064005F0032003100300031

or

H.W.I.D.9.7.......,u.s.b.\.v.i.d._.0.A.5.C.&.p.i.d._.2.1.0.1

or

HWID97   ,usb\vid_0A5C&pid_2101

And save as tosrfusb.cat (replace original file).

6. Now you can install your BT.

Vista or WS2008 x86: %PROGRAMFILES%\Toshiba\Bluetooth Toshiba Stack\ECCenter.exe

Vista or WS2008 x64: %PROGRAMFILES(x86)%\Toshiba\Bluetooth Toshiba Stack\ECCenter.exe

You must Ignore install not sign drivers and all is Okay.

[Source: MSDN Forums]

Windows Media Player Plugin for Firefox on Windows Server 2008

If anyone tried to install WMP plugin for Firefox on Windows Server 2008 (or any server os), they will get an error stating that server os are not supported. In order to get it working, you need to do the following steps.

  1. Rename wmpfirefoxplugin.exe to wmpfirefoxplugin.zip.
  2. Extract it into a directory.
  3. Type the following command in a console:
    msiexec /a ffplugin.msi
  4. It will install it probably in C:\
  5. Copy this file, np-mswmp.dll, into your plugins directory of Firefox (usually C:\Program Files\Mozilla Firefox\plugins)
Restart Firefox and it should work.

DevTeach is coming to Toronto in just a few more weeks!

I copied this blog post from Chris Dufour's blog. Chris copied it from Rob Windsor's blog. Rob copied it from Julie Lerman's blog. Julie copied it from Guy Barrette's blog. I'm sure someone will copy it from me and add their own little flair.

If you live in Toronto and don't attend DevTeach, Guy Barrette is going to make you code in Clipper for the remainder of your career (BTW - that's Clipper development with no Multi Edit and no Norton Guides. Ah, there, now you're shaking).  Seriously, DevTeach has a great lineup of speakers from Toronto and across the globe. Where else can you hear, see, touch, feel, talk to, describe your problems (IT/Dev related or not) and have a beer with these guys/gals?

And that's only half of them!!!

Need more reasons?

Keynote by Scott Hanselman
Scott is one of the most prolific, renowned and respected bloggers (
http://www.hanselman.com) and podcasters (http://www.hanselminutes.com) in the .NET world. Scott is a hands-on thinker, a renowned speaker and writer. He has written a few books, most recently with Bill Evjen and Devin Rader on Professional ASP.NET. In July 2007, he joined Microsoft as a Senior Program Manager in the Developer Division. In his new role he'll continue to explore and explain a broad portfolio of technologies, both inside and outside Microsoft. He aims to spread the good word about developing software, most often on the Microsoft stack. Before this he was the Chief Architect at Corillian Corporation, now a part of Checkfree, for 6+ years and before that he was a Principal Consultant at STEP Technology for nearly 7 years.
http://www.devteach.com/keynote.aspx

Silverlight 2.0 Workshop
For the first time an independent conference is having a workshop on Building Business Applications with Silverlight 2.0.  Join Rod Paddock and Jim Duffy as they give you a head start down the road to developing business-oriented Rich Internet Applications (RIA) with Microsoft Silverlight 2.0. Microsoft Silverlight 2.0 is a cross-browser, cross-platform, and cross-device plug-in positioned to revolutionize the way next generation Rich Internet Applications are developed. Microsoft’s commitment to providing an extensive platform for developers and designers to collaborate on creating the next generation of RIAs is very clear and its name is Silverlight 2.0. In this intensive, full-day workshop, Rod and Jim will share their insight and experience building business applications with Silverlight 2.0 including a review of some of the Internet’s more visible Silverlight web applications. This workshop is happening on Friday May 16 at the Hilton Toronto.
http://www.devteach.com/PostConference.aspx#PreSP

Bonus session: .NET Rocks hosts a panel May 14th at 18:00
This year the bonus session (Wednesday May 14 at 18:00) will be a panel of speakers debating the Future of .NET. Where is .NET going? How will new development influence .NET and be influenced by .NET? Join Carl Franklin and Richard Campbell from .NET Rocks as they moderate a discussion on the future directions of .NET. The panellists include individuals who have strong visions of the future of software development and the role that .NET can play in that future. Attend this session and bring your questions to get some insight into the potential future of .NET! This bonus session is free for everyone. Panelists are: Ted Neward,Oren Eini ,Scott Bellware
http://www.devteach.com/BonusSession.aspx

Party with Palermo, DevTeach Toronto Edition
Jeffrey Palermo (MVP) is hosting Monday May 12th in Toronto is acclaimed "Party with Palermo". This is the official social event  kicking off DevTeach Toronto. The event is not just for the attendees of Toronto it’s  a free event for everyone. It’s a unique chance for the attendees, speakers and locals  to meet and talk with a free beer.   The event will be held at the Menage club  location and you need to RSVP to attend. Get all the details at this link:
http://www.partywithpalermo.com/

DevTeach Toronto is going to be a lot of fun and a great chance to learn from the best minds in the industry.  Register now - you don't want to miss out.

Toronto Code Camp Interview

Well, 45 minutes before my surprise presentation I had to do, without me even knowing I had to do it. Here it is.


Video: Toronto Code Camp 2008 - Speakers Room Part 2

Singularity Source Code Released to CodePlex

From OSNews:

Microsoft has released source code from the Singularity research project onto Codeplex under an academic, non-commercial license. "The Singularity Research Development Kit is based on the Microsoft Research Singularity project. It includes source code, build tools, test suites, design notes, and other background materials. The Singularity RDK is for academic non-commercial use only and is governed by this license."

How cool is that? I'm going to download it and play with it! For those who don't already know, here's a description of what Singularity is.

Singularity is a research project focused on the construction of dependable systems through innovation in the areas of systems, languages, and tools. We are building a research operating system prototype (called Singularity), extending programming languages, and developing new techniques and tools for specifying and verifying program behavior.


Advances in languages, compilers, and tools open the possibility of significantly improving software. For example, Singularity uses type-safe languages and an abstract instruction set to enable what we call Software Isolated Processes (SIPs). SIPs provide the strong isolation guarantees of OS processes (isolated object space, separate GCs, separate runtimes) without the overhead of hardware-enforced protection domains. In the current Singularity prototype SIPs are extremely cheap; they run in ring 0 in the kernel’s address space.


Singularity uses these advances to build more reliable systems and applications. For example, because SIPs are so cheap to create and enforce, Singularity runs each program, device driver, or system extension in its own SIP. SIPs are not allowed to share memory or modify their own code. As a result, we can make strong reliability guarantees about the code running in a SIP. We can verify much broader properties about a SIP at compile or install time than can be done for code running in traditional OS processes. Broader application of static verification is critical to predicting system behaviour and providing users with strong guarantees about reliability.

Presentation: Overview of ASP.NET 3.5 Extensions

The presentation I did for the Toronto Code Camp, "Overview of ASP.NET 3.5 Extensions", is up on my site.

Download it here.

SharePoint Designer Bug With Creating Data View Web Parts

For those who has been getting the ultimate crashes from SharePoint Designer, and many weird oddities with it, this is something that you should know while working with Data View Web Parts. Occasionally, SharePoint Designer will NOT allow you to add a Data View Web Part no matter what you do, as shown below, and the main reason is... *drum roll*... your file is not saved.

The first screen shot shows a "changed but not saved file" and "Insert Selected Fields As..." not working and the second screen shot shows the "Insert Selected Fields as..." working after saving.

SharePoint Tip: Displaying HTML Encodes Properly

Here's a tip for those people getting &amp; instead of & (and all those other fancy html encodes) from your XSLT variables. You just need to add disable-output-escaping and set that to "yes" when you do a value-of.

<xsl:value-of select="$Site" disable-output-escaping="yes"/>