Archive for the ‘Software Development’ Category

Forwarding events in C#

Sunday, March 7th, 2010

For a long time I’ve had a problem with events in C#: I want to wrap one class in another, which offers a higher level of abstraction, but this means that all the events have to be copied and lots of boilerplate code has to be written to do the forwarding. The obvious thing to do would be to have an event exposed as a property, which would give clients of the abstract interface read-only access to the event:

public class HigherLevelClass
{
   public event SomeDelegate MyEvent
   {
      get { return impl.MyEvent; }
   };

   private LowerLevelClass impl;

   /* ... */
}

Unfortunately this isn’t valid syntax, and at first glance that seems to be the end of it. However, I discovered today that the designers of C# have actually thought of this, and provided a way round it:

public class HigherLevelClass
{
   public event SomeDelegate MyEvent
   {
      add { impl.MyEvent += value; }
      remove { impl.MyEvent -= value; }
   };
}

Simple, once you know how. But this illustrates an interesting problem with keyword search when applied to programming techniques: I’ve searched for just this information on several occasions, but wasn’t able to find it until I hit on the magic phrase “event forwarding”. In my experience it’s pretty common to know how to describe what you’d like something to do, but not be able to find the same words as the person who knows how to do it.

I’m sure this is far from the only reason, but perhaps it goes some way to explaining why there are eight separate implementations of every single open source library type, each of which is partially complete.

You were never meant to do that with SQL

Wednesday, December 30th, 2009

There seems to be a lot of hatred for SQL in the world at the moment: I can’t think of any other reason why the term NoSQL would catch on in the way that it has, when the key technological distinction is actually the lack of ACID guarantees (which are entirely orthogonal to whether or not SQL is used, as evidenced by non-ACID MySQL and HiveQL, which offers a pretty familiar SQL-like interface on an entirely non-traditional backend).

I wonder whether one of the unspoken reasons for this hatred is that at one point or another almost everyone has ended up doing this sort of thing:

   builder.Add("SELECT foo FROM bar WHERE id = ");
   builder.Add(id.ToString());

   if (additionalConstraint)
   {
      builder.Add(" AND frobbable = 1 ");
   }

   /* ... ad nauseam ... */

SQL is a hard language to like: it’s never been properly standardised (or rather, it has, but the standard has never been implemented) meaning that you spend too much time worrying about compatibility. Its theoretical underpinning is poor, leading to constructions that are hard for the engine to optimise (meaning more manual work).

However, SQL is a language in its own right, and was never intended to be generated programmatically by another programming language. This shouldn’t come as a surprise, as I struggle to think of any programming language that has been designed to work in this way.

Using SQL from a decent command-line environment is a powerful tool and often a pleasure to use. By comparison, generating SQL programmatically is an abomination that would be worth of The Daily WTF were it not for the fact that nobody’s ever invented an API that offers the same flexibility.

Personally, I blame the vendors. Until RDBMSs can offer the same quality of optimisation that modern compilers can (that is, write in a high-level language and never even think about micro-optimisation) high-performance relational database access will remain a sea of vendor-specific optimiser hacks. Maybe there’s a theoretical reason why optimisers will never be this good, in which case perhaps we do need to abandon the relational model in practice. But let’s not pretend it has anything to do with SQL.

Project Euler in F#: Problem 8

Tuesday, December 22nd, 2009

I’ve been trying to teach myself F# using the Project Euler problems, and I’m starting to feel I’m getting somewhere with the language. The few Euler problems I’ve solved so far have had very straightforward and natural solutions.

Problem 8 is as follows: Find the subsequence of 5 consecutive digits that yield the greatest product when multiplied together, in the 1000-digit number:

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

I was able to come up with an F# solution that is one line, plus a helper line to convert the string into a sequence of digits:

let str = "731<...>"

let digits = Seq.map (fun x -> int (Char.GetNumericValue x)) str

let maxproduct num list =
 Seq.max (Seq.map (fun x -> Seq.reduce (*) x) (Seq.windowed num list))

The value digits is just a list of the digits in the string, converted into integers. The function maxproduct works the obvious way: take every subsequence of five digits (Seq.windowed), multiply them together (Seq.reduce, applied to each element of the sequence with Seq.map) and then find the maximum (Seq.max).

The only reason this needs quite so little work is the existence of Seq.windowed in the standard library, which does exactly the right thing in turning a 1000-element list into 996 arrays of subsequences of consecutive digits.

I’m not sure I like ramming all the functions into one line, and I’m sure there must be a way to combine map and reduce without the lambda, which adds a lot of clutter. If this was real code, it would need quite a lot of work to make it readable. However, the standard library is a big win, because the process of ‘windowing’ a sequence is nicely separated from the code. It’s also nice (for toy problems like this, at any rate) that the program is pretty much a definition of the problem, with little thought being necessary as to how to do the processing.