Project Euler (pronounced "Oiler") provides a number (~182) of challenging math/programming problems... which is a fantastic environment to play with F#!
I've only completed 6% of the questions but I've already learnt a lot. (F# is huge)
If you have some free time (or get bored during your compiles), I'd definitely recommend having a go - in your own language of choice of course!
Wednesday, February 20, 2008
Tuesday, January 15, 2008
Deriving Miss Daisy
As I've mentioned previously, I'm slowly reading through SICP - it's an amazing book and reading it has been a humbling experience.
'Geeking' Out
I completely geeked out reading one example on functional derivatives. (Take a math expression as a parameter and return the derivative as an expression)
It's in the first chapter, and they use this to implement Newton's Method -> to then implement a Sqrt function!
Approximate Derivative
After I picked my brain off the floor, I decided to have a crack at implementing derivatives in C# using lambda expressions:

Using the formula of a derivative (above), we can simply code an approximate derivative (below):
Symbolic Derivation
Easy huh! So for fun I decided to use linq expression trees to derive algebraically.
It's easy to get an expression tree in C# - simply use the Expression<> type.
I parse the expression tree using Matt Warren's ExpressionVisitor class - and using the basic rules of Derivation (Constant, Product and Quotient rules) we get:
Wrapping it up
To quickly finish this long blog post, here's the usage code:
And there it is! Useful? maybe not. Fun? Definitely!
'Geeking' Out
I completely geeked out reading one example on functional derivatives. (Take a math expression as a parameter and return the derivative as an expression)
It's in the first chapter, and they use this to implement Newton's Method -> to then implement a Sqrt function!
Approximate Derivative
After I picked my brain off the floor, I decided to have a crack at implementing derivatives in C# using lambda expressions:
Using the formula of a derivative (above), we can simply code an approximate derivative (below):
Func<double, double> Approximate(Func<double, double> f)
{
var h = 0.0001;
return a => ((f(a + h) - f(a)) / h);
}
Symbolic Derivation
Easy huh! So for fun I decided to use linq expression trees to derive algebraically.
It's easy to get an expression tree in C# - simply use the Expression<> type.
Func<double, double> Derive(Expression<Func<double, double>> e)
{
var lambda = new Derivative().Eval(e) as LambdaExpression;
return (Func<double, double>) lambda.Compile();
}
I parse the expression tree using Matt Warren's ExpressionVisitor class - and using the basic rules of Derivation (Constant, Product and Quotient rules) we get:
class Derivative : ExpressionVisitor
{
public Expression Eval(Expression exp)
{
return this.Visit(Evaluator.PartialEval(exp));
}
protected override Expression VisitBinary(BinaryExpression b)
{
// Product Rule: (fg)' = f'g + fg'
if (b.NodeType == ExpressionType.Multiply)
{
return Expression.Add(
Expression.Multiply(Eval(b.Left), b.Right),
Expression.Multiply(b.Left, Eval(b.Right)));
}
// Quotient Rule: (f/g)' = (f'g - fg') / (g*g)
if (b.NodeType == ExpressionType.Divide)
{
return Expression.Divide(
Expression.Subtract(
Expression.Multiply(Eval(b.Left), b.Right),
Expression.Multiply(b.Left, Eval(b.Right))),
Expression.Multiply(b.Right, b.Right));
}
return base.VisitBinary(b);
}
// Parameter Derivation: f(x)' = 1
protected override Expression VisitParameter(ParameterExpression p)
{
return Expression.Constant(1.0);
}
// Constant Rule f(a)' = 0
protected override Expression VisitConstant(ConstantExpression c)
{
return Expression.Constant(0.0);
}
}
Wrapping it up
To quickly finish this long blog post, here's the usage code:
// f(x * x * x)' = 3 * x * x
var approx = Approximate(x => x * x * x);
var derive = Derive(x => x * x * x);
var y1 = approx(1); // 3.0000300001109532
var y2 = derive(1); // 3.0
And there it is! Useful? maybe not. Fun? Definitely!
Monday, January 7, 2008
The 'Y' in Blog
It's quite demoralizing when you consider how much you don't know. I am continually reminded that I underestimate it's sheer quantity... it's a sad realization.
Since there is so much already written by people more knowledgeable and intelligent than myself, why do I write?
Well, I started this blog to show my appreciation, and hopefully to contribute something back to the development community.
So kudos to all of you... keep sharing the love (of maths & programming)!
Happy New Year!
Since there is so much already written by people more knowledgeable and intelligent than myself, why do I write?
Well, I started this blog to show my appreciation, and hopefully to contribute something back to the development community.
So kudos to all of you... keep sharing the love (of maths & programming)!
Associate with people who are searching for answers, avoid those who have found them.
Happy New Year!
Friday, December 7, 2007
MSDTC + Linq to Sql Update
When I was writing my previous article, I was keenly aware that it had it's limitations. One in particular was using an already open DataContext.
Take the following code:
Since it was created outside the TransactionScope I have no way of automatically sharing these connections.
To help solve this problem (cleanly) I added a SharedConnectionScope class and a UseExistingConnection call (shown below) - both take an existing DataContext.
Caveat: It only works with one 'external' connection, but that's still better than none.
You can grab the updated code here.
Take the following code:
// Create outside TransactionScope
var c1 = new UserDataContext();
using (var scope = new TransactionScope())
{
c1.Users.ToList();
// This will promote to MSDTC
using (var c2 = new UserDataContext())
c2.Users.ToList();
scope.Complete();
}
To help solve this problem (cleanly) I added a SharedConnectionScope class and a UseExistingConnection call (shown below) - both take an existing DataContext.
// Create outside TransactionScope
var c1 = new UserDataContext();
using (var scope = new TransactionScope())
using (SharedDataContext.UseExistingConnection(c1))
{
c1.Users.ToList();
// This will share existing connection
using (var c2 = new UserDataContext())
c2.Users.ToList();
scope.Complete();
}
Caveat: It only works with one 'external' connection, but that's still better than none.
You can grab the updated code here.
Thursday, December 6, 2007
Quick Application - Tickle Me Pink
I do all my WPF coding in straight XAML, and for my prototypes I like to use the standard .Net colors.
So instead of playing a fun colorname -> color guessing game, I thought I'd have some fun and whip up this tiny color app (it's ridiculously easy in WPF).
The following code is the entire application, it combines WPF, reflection, Linq and Anonymous Types.
UPDATE: Adjusted layout to look better in google reader.
<Window x:Class="Colors.ColorWindow" Title="Colors"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:Colors" WindowStyle="ToolWindow"
Height="500" Width="200">
<Window.Resources>
<DataTemplate x:Key="ColorTemplate">
<StackPanel Orientation="Horizontal">
<Border BorderBrush="Black" BorderThickness="1" Width="30" Height="30" Margin="10"
Background="{Binding Brush}" ToolTip="{Binding Brush.Color}" />
<TextBlock Text="{Binding Name}" VerticalAlignment="Center" />
</StackPanel>
</DataTemplate>
</Window.Resources>
<ListBox x:Name="colors" ItemTemplate="{StaticResource ColorTemplate}" />
</Window>
public partial class ColorWindow : Window
{
public ColorWindow()
{
InitializeComponent();
colors.ItemsSource = typeof(Brushes)
.GetProperties(BindingFlags.Static | BindingFlags.Public)
.Select(x => new {
Name = x.Name,
Brush = x.GetValue(null, null)
});
}
}
Tuesday, December 4, 2007
DataModel-View-ViewModel
WPF changed the rules for UI best practice, but it didn't update the book.
With all the new and improved DataBinding and Command features - a new set of patterns have emerged, including a recommended pattern called DataModel-View-ViewModel (DM-V-VM).
At a conceptual level this pattern looks fantastic – but there are issues... namely it doesn’t work for non-trivial scenarios.
Rather than a boring high level rant, I'll use boring examples to explain why:
Cannot support events
The recommended approach is to use DataTemplates and Commands - but DataTempates cannot handle events, and the command system is not extensive enough (eg. doesn't support mouse clicks).
Supporting events requires using code behind; which means using at least a UserControl base class.
No Keyboard support for Standard Commands
Dan Crevier and John Gossman don't mention the standard Commands, instead opting to create their own – not because it's better practice, but it's easier to demonstrate.
Their suggested approach uses CommandParameter bindings on the buttons, which only works with OnClick (i.e when the shortcut Key is pressed "null" is passed through).
Since the InputBindings.KeyBinding.CommandParameter does not support DataBinding - supporting the keyboard (and still using commands) means we can't use CommandParameters.
UI support breaks abstraction
Say that we want to delete all the selected items from a list; this means storing a list of items in the ViewModel (since we not using CommandParameters), which we then bind from our View.
But this is purely for UI logic rather than business logic!
Complicated scenarios might need multiple properties - and since the binding is not clearly defined, it will introduce bugs.
The abstraction is now broken, so let's just move our ViewModel into the code behind.
Unit testing the UI logic now not easy
No kidding, that's why we initially moved the code into the ViewModel!
All of the real logic should be implemented in the data bound objects or by a class separate to the UI. These classes are unit testable, but the UI integration is not.
Aren't we back to where we started?
Not quite, WPF's new style allows us to develop a cleaner system than the days of WinForms and MFC - and while there's not a published best practice for it, it doesn't mean that it doesn't exist.
UI programming is hard, especially since it looks so easy.
With all the new and improved DataBinding and Command features - a new set of patterns have emerged, including a recommended pattern called DataModel-View-ViewModel (DM-V-VM).
At a conceptual level this pattern looks fantastic – but there are issues... namely it doesn’t work for non-trivial scenarios.
Rather than a boring high level rant, I'll use boring examples to explain why:
Cannot support events
The recommended approach is to use DataTemplates and Commands - but DataTempates cannot handle events, and the command system is not extensive enough (eg. doesn't support mouse clicks).
Supporting events requires using code behind; which means using at least a UserControl base class.
No Keyboard support for Standard Commands
Dan Crevier and John Gossman don't mention the standard Commands, instead opting to create their own – not because it's better practice, but it's easier to demonstrate.
Their suggested approach uses CommandParameter bindings on the buttons, which only works with OnClick (i.e when the shortcut Key is pressed "null" is passed through).
Since the InputBindings.KeyBinding.CommandParameter does not support DataBinding - supporting the keyboard (and still using commands) means we can't use CommandParameters.
UI support breaks abstraction
Say that we want to delete all the selected items from a list; this means storing a list of items in the ViewModel (since we not using CommandParameters), which we then bind from our View.
But this is purely for UI logic rather than business logic!
Complicated scenarios might need multiple properties - and since the binding is not clearly defined, it will introduce bugs.
The abstraction is now broken, so let's just move our ViewModel into the code behind.
Unit testing the UI logic now not easy
No kidding, that's why we initially moved the code into the ViewModel!
All of the real logic should be implemented in the data bound objects or by a class separate to the UI. These classes are unit testable, but the UI integration is not.
Aren't we back to where we started?
Not quite, WPF's new style allows us to develop a cleaner system than the days of WinForms and MFC - and while there's not a published best practice for it, it doesn't mean that it doesn't exist.
UI programming is hard, especially since it looks so easy.
Monday, December 3, 2007
Avoiding MSDTC with Linq to Sql
Now MSDTC is not evil; it's just overkill when it isn't needed. It slows performance, increases complexity and requires client side configuration... and it can activate when you least expect it.
Take the following code:
This code uses the recommended (and very helpful) TransactionScope class - and unfortunately invokes MSDTC.
Since we're using separate instances of UserDataContext this creates separate database connections - and causes TransactionScope to "promote" the transaction to require MSDTC.
Avoiding this is as simple as sharing the database connection - after all, it is the same database - but passing database connections to sub methods gets ugly really quick.
My Solution
To solve this problem in a cleaner manner, I wrote a Transaction Resource Manager - which works behind the scenes and makes the above code work without change.
It simply shares database connections across the instances (only within a transaction). It's thread safe, and easy to integrate - simply change the DataContext base class in the dbml designer.

Grab the code and check it out. There's an example program and a whole slew of unit tests (around 275 - testing the various permutations of nested TransactionScopes).
Kudos to Nick Guerrea's Weak Dictionary - which I use to weakly track the transactions and share the connections.
Same Solution for other Scenarios
There are other scenarios where this problem arises:
Say you've got a database with many tables (or a plug-in system that shares a database) - a modular approach uses multiple DataContext classes.
Our problem occurs when you need to modify tables from different DataContexts within a single transaction.
Thankfully, the root cause is still the same and our solution solves these cases as well.
This has been a great learning experience for me, and I must say that the Transaction Resource Managers are very interesting (might even make a good alternate ScopeGuard implementation?)
EDIT: See updated article for extra goodness.
Take the following code:
void CreateUsers()
{
using (var scope = new TransactionScope(TransactionScopeOption.Required))
{
CreateUser("Fred");
CreateUser("Joe");
scope.Complete();
}
}
void CreateUser(string name)
{
using (var context = new UserDataContext())
{
context.Users.InsertOnSubmit(new User { name = name });
context.SubmitChanges();
}
}
This code uses the recommended (and very helpful) TransactionScope class - and unfortunately invokes MSDTC.
Since we're using separate instances of UserDataContext this creates separate database connections - and causes TransactionScope to "promote" the transaction to require MSDTC.
Avoiding this is as simple as sharing the database connection - after all, it is the same database - but passing database connections to sub methods gets ugly really quick.
My Solution
To solve this problem in a cleaner manner, I wrote a Transaction Resource Manager - which works behind the scenes and makes the above code work without change.
It simply shares database connections across the instances (only within a transaction). It's thread safe, and easy to integrate - simply change the DataContext base class in the dbml designer.

Grab the code and check it out. There's an example program and a whole slew of unit tests (around 275 - testing the various permutations of nested TransactionScopes).
Kudos to Nick Guerrea's Weak Dictionary - which I use to weakly track the transactions and share the connections.
Same Solution for other Scenarios
There are other scenarios where this problem arises:
Say you've got a database with many tables (or a plug-in system that shares a database) - a modular approach uses multiple DataContext classes.
Our problem occurs when you need to modify tables from different DataContexts within a single transaction.
Thankfully, the root cause is still the same and our solution solves these cases as well.
This has been a great learning experience for me, and I must say that the Transaction Resource Managers are very interesting (might even make a good alternate ScopeGuard implementation?)
EDIT: See updated article for extra goodness.
Subscribe to:
Posts (Atom)