Richard's coding blog

Test posting

Posted on 5 September 2011 at 11:23am Pacific time.

Fun with contracts and anonymous delegates

I officially blew my own mind today. I discovered System.Diagnostics.Contracts and inline declaration and invocation of anonymous delegates all in the same day...

namespace FunWithContracts
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.Contracts;
    using System.Linq;

    internal static class Program
    {
        private static void MySort<T>(T[] array, int index, int length, IComparer<T> comparer)
        {
            Contract.Requires<ArgumentNullException>(array != null);

Binding "this" in JavaScript: I finally get it!

var obj = {
  name: "Richard",
  callback: function(event) {
    print("event = " + event);
    print("name  = " + this.name);
  }
};
var element = {
  name: "Element",
  callback: null,
  invoke: function() {
    print("invoke");
    if (this.callback != null) {
      this.callback("hello");
    }
  }
};
function bind(context, func) {
  return function() {
    return func.apply(context, arguments);
  }
}
element.callback = bind(obj, obj.callback);
element.invoke();

Simulating packet loss with iptables/ipfw

I need to simulate packet loss for a little programming assignment I'm working on which involves implementing a multicast chat application using UDP sockets. I need to build reliability into the system (sequence numbers and acknowledgements etc.) and thought it might be sensible to test the behaviour of my program under WAN-style network conditions. Originally I was just going to randomly drop packets in my server, but then I thought to myself that there ought to be an OS-level way of doing it. Indeed, there is and here is my bash script that demonstrates it.

Mono C# extensibility project

I have created the "Mono C# extensibility project" with the intention of making it straightforward to write custom add-ins for the Mono C# compiler.

Here's a link: http://github.com/rcook/mono-extensibility/

Instrumenting IL generation in Mono's C# compiler

Someone asked me the other day how I'd go about instrumenting the IL generated by the Mono C# compiler: something to do with custom profiling I think. I still haven't come up with a totally satisfactory answer but here's a patch of some work I did showing how I hacked away at various bits of the sources to emit custom method prologues and epilogues. Download the patch and apply it to your Mono Git repo.

Visitor pattern (re)visited

I am aware that the visitor design pattern has been around in some form for years and so what I'm about to write in this post is not particularly innovative or special in any way. I just thought I'd discuss something I've been pondering for a while in my attempts to come up with interesting refactorings for the DuctileJ project.

Anyway, I read

Syndicate content