Monday, 23 January 2012

New Year, New Language

Functional programming languages are all the rage at the moment. They’re well-suited to parallel programming  and the multi-core world. On the Microsoft .NET platform we have F#. I’ve made one or two attempts at learning F# before but lost heart once the going got tough. This time around I’ve decided to make more of an effort. I’ve found that it helps to try more than one learning source as they differ in the degree of explanation they give for each concept.

Thus far I am consulting primarily F# Programming, Real World Functional Programming (online partial version of the book) and MSDN’s F# Language Reference.

I’ve been thinking about whether the learning-curve from procedural to object-oriented programming is greater than that from OO (or procedural) to functional.

I think the harder part about going from procedural to OO was not the mechanics but OO design perhaps. Whereas with functional I think even the mechanics are quite difficult.

However, it could be that I’ve just forgotten how difficult the procedural to OO transition was!

One initial difficulty with F#, especially for those coming from a C-syntax background, is F#’s syntax. It does look quite alien. Syntax itself should not be that big a deal but when combined with new concepts it does add to the mental load, especially once examples start to get elaborate.

A similar language on the Java JVM is Scala. Its syntax is a cross between C-syntax and Ruby/Python’s. I looked briefly at Scala some time ago and it does seem more accessible initially. Though once you get beyond the basics it becomes as scary as F#! A colleague of mine who’s been using Scala commercially for many months tells me it’s a matter of practice. Blogger Labels: Functional,.NET,Microsoft,F#,Java,Scala,Ruby,Python

Thursday, 7 July 2011

Reactive Extensions 1.0 Stable is Released

Some months ago Microsoft made Reactive Extensions (Rx) an officially supported product and moved it out of Dev Labs to its new site. On June 29th it was officially released as version 1.0.  It now also has some very accessible starter documentation in MSDN. Until now documentation has been scattered between videos, blogs, hands on labs and the MSDN Rx forum.

Rx is also consumable from LINQPad. LINQPad subscribes to the observables that you dump. For example the example below writes “Hello World” every second but stops after the first five. If we removed the call to Take(5) it would run for ever. In that case you can stop it by hitting the Stop button in LINQPad.

image

Thursday, 30 June 2011

Leveraging LINQPad’s Object Visualisation in Visual Studio

When you run a query in LINQPad it produces formatted output on the results tab that you can optionally export to Word, Excel or HTML. Here I show two techniques for visualising any arbitrary .NET object.

The first way is non-invasive, i.e., requires no change to your Visual Studio solution or compiled assemblies.

The second way is invasive but allows you to export any arbitrary .NET object to HTML by leveraging LINQPad’s Dump() extension method.

Technique 1: Add a Visual Studio LINQPad Debugger Visualizer

Download the LINQPad Visualizer from Google Code and follow the instructions from the point where it says “If using the download do this:.”

You should also remember to unblock the DLLs after downloading. Consider this simplified code with fields made public for brevity.

class User
{
public int UserId;
public List<Course> Courses;
public List<Student> Students;
}

class Course
{
public string CourseCode;
}

class Student
{
public int StudentId;
}


Set up a collection of Users.



var users = new List<User> {
new User {
UserId = 1,
Courses = new List<Course> {
new Course {
CourseCode = "ECU120"
},
new Course {
CourseCode = "ECU121"
},
new Course {
CourseCode = "ECU122"
},
},
Students = new List<Student> {
new Student {
StudentId = 1
},
new Student {
StudentId = 2
},
}
},
new User {
UserId = 2,
Courses = new List<Course> {
new Course {
CourseCode = "ECU124"
},
new Course {
CourseCode = "ECU125"
},
new Course {
CourseCode = "ECU126"
},
},
Students = new List<Student> {
new Student {
StudentId = 1
},
new Student {
StudentId = 3
},
}
},
new User {
UserId = 3,
Courses = new List<Course> {
new Course {
CourseCode = "BESU022"
},
new Course {
CourseCode = "BESU023"
},
new Course {
CourseCode = "ECT034"
},
},
Students = new List<Student> {
new Student {
StudentId = 1
},
new Student {
StudentId = 2
},
}
},
};

users.Dump();



Then we can debug into it.



image



To do this set a watch for your object of interest. Here we are inspecting the users collection. In order to view its contents we need to enter



new System.WeakReference(users)



in the Name column as shown.



Clicking on the dropdown for Value shows two LINQPad debugger visualizers. The default visualizer is a JSON visualizer that allows us to see the contained values of objects that haven’t been marked as Serializable. Clicking on the visualizer pops up a Windows Form containing the object’s values.



image



In this case we lose the type information but we can still see all the values neatly laid out.



However, if we mark the User, Student and Course classes as Serializable…



[Serializable]
class User
{
public int UserId;
public List<Course> Courses;
public List<Student> Students;
}

[Serializable]
class Course
{
public string CourseCode;
}

[Serializable]
class Student
{
public int StudentId;
}



Then select the lower LINQPad visualizer we get the type information as well and with neater layout.



image



Technique 2: Add a Reference to LINQPad.exe



For this technique we add a reference to LINQPad.exe that allows us to leverage LINQPad’s Dump() extension method for any object…



/// <summary>
///
LINQPad extension methods.
/// </summary>
public static class LinqPadExtensions
{
/// <summary>
///
Writes object properties to HTML
/// and displays them in default browser.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="o"></param>
/// <param name="heading"></param>
public static void Dump<T>(
this T o,
string heading = null
)
{
string localUrl =
Path.GetTempFileName() + ".html";
using (
var writer =
LINQPad.Util.CreateXhtmlWriter(true)
)
{
if (!String.IsNullOrWhiteSpace(heading))
writer.Write(heading);
writer.Write(o);
File.WriteAllText(localUrl, writer.ToString());
}
Process.Start(localUrl);
}
}



Then we can write users.Dump() as in the example above. On execution this writes the object’s values to HTML and launches the default web browser. This produces the same results as the typed version above.



image



This method has been adapted from an example on StackOverflow. One issue to be aware of is that your project should target the full .NET 4, not the Client Profile.

Thursday, 18 November 2010

ASP.NET MVC and LINQ to SQL – LINQPad to the Rescue

Recently I was rewriting  a broken algorithm in some ASP.NET MVC code.  For part of the fix I needed to write some LINQ to SQL. I needed to do a comparison against a nullable field but I was thinking in C# not in SQL. Here is a simplified version of the real problem. Suppose we have a table of customers where LastName is a nullable field. We want to return customers whose last name is NULL.

string lastName = null;

var customers =
from c in Customers
where c.LastName == lastName
select c;


This was unexpectedly returning no values. The correct solution is



var customers =
from c in Customers
where c.LastName == null
select
c;


The reason was due to the “IS NULL vs. = NULL”  issue in the underlying SQL. This was easy to check by launching the excellent LINQPad application that allows you to view the underlying SQL after running a LINQ statement. I’d just assumed that from the C#/LINQ side it would automatically generate the appropriate code.



Comparing directly against null from the C# generates



SELECT [t0].[CustomerID], [t0].[FirstName], [t0].[LastName]

FROM [Customers] AS [t0]

WHERE [t0].[LastName] IS NULL



Comparing against a variable that’s null generates



DECLARE @p0 VarChar(1000) = null

SELECT [t0].[CustomerID], [t0].[FirstName], [t0].[LastName]

FROM [Customers] AS [t0]

WHERE [t0].[LastName] = @p0



In my real problem I was comparing against a nullable ID column and I wanted to return both null and non-null matches in one clause of a compound where. So I wanted to do a more elaborate version of this.



string name = (lastName != null) ? "Fred" : null;

var customers =
from c in Customers
where c.LastName == name
select c;


But I was forced to use an if-else instead.



if (lastName != null)
{
var customers =
from c in Customers
where c.LastName == lastName
select c;
// ...more code
}
else
{
var customers =
from c in Customers
where c.LastName == null
select
c;
// ...more code
}


I couldn’t think of a more concise way around this. Anyway, at least I solved the bigger problem of which this was a part. :)

Wednesday, 25 August 2010

JavaScript Unit Testing with QUnit

Recently, I’ve been trying to learn more about the Reactive Extensions library (currently still in Microsoft DevLabs) which I’ve blogged about previously. Since then Microsoft have pushed out a JavaScript version of the library and published a couple of Hands-on Labs for both the .NET and JavaScript versions. I thought I’d start looking at the JavaScript version as well. However, since the lab makes use of the jQuery JavaScript library it helps to be familiar with it too. So I’ve spent some time with jQuery as well. This in turn led me to realise that I needed to learn more about JavaScript in general.

In following Matthew Podwysocki’s Rx for JavaScript tutorials (not got very far yet) I noticed that he makes use of QUnit, a JavaScript unit testing library that is an offshoot of jQuery. So I made a diversion into this. It turns out that there are a number of unit testing libraries for JavaScript, but I’d not bothered to look into them before. Although QUnit is an offshoot of jQuery, and is what jQuery itself uses to test jQuery, it can be used for generic JavaScript. I have a number of JavaScript utility methods so after an initial play with QUnit I thought I’d try creating a few tests. Lo and behold I discovered a bug in the first method I tried.  Ah, the benefits of unit testing.

There’s a one-page runnable example on the home page and a couple of tutorial links. Following the lead of the second tutorial I created a web site in Visual Studio, added the tester page and then separate “project under test” and unit tests JavaScript files.

<script type="text/javascript" src="Scripts/qunit.js"></script> 
<!-- Library -->
<script type="text/javascript" src="Scripts/utility.js"></script>
<!-- Tests -->
<script type="text/javascript" src="Scripts/tests.js"></script>


I have a method called isInteger(text) that checks whether a text entry is a [positive] integer. Its unit tests are:



/// <reference path="utility.js" />
module('isInteger Tests');

test('isInteger_positiveInteger_true', function () {
var text = '32';
ok(isInteger(text), text + ' is an integer');
})

test('isInteger_negativeInteger_false', function () {
var text = '-32';
ok(!isInteger(text), text + ' is not an integer');
})

test('isInteger_zero_true', function () {
var text = '0';
ok(isInteger(text), text + ' is an integer');
})

test('isInteger_decimal_false', function () {
var text = '32.3';
ok(!isInteger(text), text + ' is not an integer');
})

test('isInteger_alpha_false', function () {
var text = 'abc';
ok(!isInteger(text), text + ' is not an integer');
})

test('isInteger_alphanumeric_false', function () {
var text = 'abc123';
ok(!isInteger(text), text + ' is not an integer');
})

test('isInteger_whitespace_false', function () {
var text = ' ';
ok(!isInteger(text), 'Whitespace text is not an integer');
})

test('isInteger_empty_false', function () {
var text = '';
ok(!isInteger(text), text + 'Empty text is not an integer');
})



QUnit produces out put like this (showing tests 1 to 5 for isInteger with some tests collapsed):



image



This is just the simplest usage of QUnit. It has some more assertions and also an asynchronous testing capability. It is this that Matthew Podwysocki uses in one of his Rx for JavaScript blog posts.

Friday, 11 June 2010

A Useful New String Method in .NET 4.0

In .NET 2.0 the String class introduced a method, IsNullOrEmpty, that allows you to check whether a string is null or the Empty string. However, sometimes you also want to disallow a string that contains just white space. I created a static utility method called IsNullOrEmptyOrBlank that additionally checked for white space.

public static bool IsNullOrEmptyOrBlank(string s)
{
return String.IsNullOrEmpty(s) || s.Trim().Length == 0;
}



In .NET 3.5 I turned this into an extension method.



public static bool IsNullOrEmptyOrBlank(this string s)
{
return String.IsNullOrEmpty(s) || s.Trim().Length == 0;
}



Then to my pleasure I noticed that in .NET 4.0 Microsoft has fixed this oversight and added this functionality to the String class.  The method is IsNullOrWhiteSpace. The description is: “Indicates whether a specified string is null, empty, or consists only of white-space characters.”



Kudos to Microsoft.  :-)

Monday, 7 June 2010

Finding the Missing Number in a Sequence

This is one of those algorithm problems that come up in programmer interview tests from time to time. Suppose you receive a sequence of numbers from 1 to 100 in random order but one is missing.  How do you find the missing number?

The laborious way would be to sort the numbers in ascending order and then loop until the difference in two successive numbers is 2. But this would be very slow if we had to sort 10 million numbers.

A slick solution I encountered was to make use of the formula for the sum of an arithmetic progression which, for a sequence of n consecutive numbers starting from 1, is n(n+1)/2. The full formula is n(2a + (n- 1)d)/2 where a is the first term and d is the common difference. If we substitute a = 1 and d = 1 then it simplifies to the first formula.

To find the missing number we simply add up the numbers from the random sequence and then subtract that sum from the sum given by the arithmetic progression formula. No sorting involved.

I thought I’d try this out using my unique random number generator class. This class takes any range of numbers and randomly shuffles them. So we can take a range of consecutive numbers from 1 to n and generate a random set. Then we can remove one at random and find it using the foregoing technique.

// Generate ordered sequence of numbers
int start = 1;
int count = 10;
var orderedNumbers =
Enumerable.Range(start, count);

// Print ordered numbers
Console.WriteLine("Ordered Numbers");
orderedNumbers
.ToList()
.ForEach(x => Console.Write(x + " "));
Console.WriteLine();

// Randomly shuffle them
var randomNumbers = new HashSet<int>();
var g = new UniqueRandomNumberGenerator(orderedNumbers);

// Print random numbers
Console.WriteLine("Random Numbers");
while (g.RemainingNumbersCount > 0)
{
int number = g.NewRandomNumber();
randomNumbers.Add(number);
Console.Write(number + " ");
}
Console.WriteLine();

// Verify we have same numbers after random shuffle
Debug.Assert(randomNumbers.SetEquals(orderedNumbers));

// Randomly generate a number to remove
var rand = new Random();
int removedNumber =
rand.Next(1, orderedNumbers.Count() + 1);
Console.WriteLine(
"Number to remove is " + removedNumber
);

// Remove it from the random numbers
randomNumbers.Remove(removedNumber);

// Print random numbers with number removed
Console.WriteLine(
"Random Numbers with {0} missing", removedNumber
);
randomNumbers
.ToList()
.ForEach(x => Console.Write(x + " "));
Console.WriteLine();

// Sum random numbers
int randomNumbersSum = 0;
var enumerator =
randomNumbers.GetEnumerator();
while (enumerator.MoveNext())
{
int number = enumerator.Current;
randomNumbersSum += number;
}

// Sum ordered numbers using arithmetic progression formula
int n = orderedNumbers.Count();
int orderedNumbersSum = n * (n + 1) / 2;

// The missing number is the difference between
// the sums of the ordered and random numbers
Console.WriteLine("Searching for missing number...");
int missingNumber = orderedNumbersSum - randomNumbersSum;
Console.WriteLine("Missing number is " + missingNumber);



I could have computed the sums using IEnumerable.Sum() on the two sets of numbers but I wanted to illustrate the algorithms.



Typical output for 10 numbers is:



Ordered Numbers

1 2 3 4 5 6 7 8 9 10


Random Numbers


10 3 5 9 1 7 4 2 8 6


Number to remove is 3


Random Numbers with 3 missing


10 5 9 1 7 4 2 8 6


Searching for missing number...


Missing number is 3