F# extension methods in Roslyn

If you scan the source code for the Roslyn project, the platform on which the next-gen C# and VB compilers are based, you might stumble across an interesting special behavior that was added in for the sole purpose of preserving backward-compatibility with F#.

From [roslyn]\Src\Compilers\CSharp\Source\Symbols\Metadata\PE\PEAssemblySymbol.cs (as of commit dc3171c8a878):

public override bool MightContainExtensionMethods
{
    get
    {
        if (!this.lazyContainsExtensionMethods.HasValue())
        {
            var moduleSymbol = this.PrimaryModule;
            var module = moduleSymbol.Module;
            // The F# compiler may not emit an assembly-level ExtensionAttribute, and previous versions of C# never checked for it.
            // In order to avoid a breaking change (while preserving the perceived performance benefits of not looking for extension
            // methods in assemblies that don't contain them), we'll also look for FSharpInterfaceDataVersionAttribute.
            var mightContainExtensionMethods = module.HasExtensionAttribute(this.assembly.Handle, ignoreCase: false) ||
                                               module.HasFSharpInterfaceDataVersionAttribute(this.assembly.Handle);

            this.lazyContainsExtensionMethods = mightContainExtensionMethods.ToThreeState();
        }

        return this.lazyContainsExtensionMethods.Value();
    }
}

The comment does a nice job of summing up the issue, but allow me to provide some additional context. Continue reading

Project Euler visualizations - problem 431

I recently solved Project Euler problem 431, which (through a bizarre and unnecessarily confusing back-story) requires one to calculate the volume trapped above a cone, inside a cylinder.  The point of the cone is taken to be level with the top of the cylinder, but the cone angle and its horizontal offset compared to the cylinder are variable.

I solved this one with Mathematica, and along the way I cobbled together a little visualization that allows one to easily see the curve of intersection where the cone and the cylinder join together.  Why not take this opportunity to share the visualization via an embedded CDF?  I've been wanting to try this for a while. Continue reading

Tracking a new year's resolution with F# and FSharpChart

This year, I have a goal of running 500 miles.  That's not a crazy-ambitious goal, but between work, school, hobbies, friends, and (occasional) downtime, I think it's plenty for me.  In contrast, the CEO of RunKeeper is planning to run 1,500 miles this year!  That's an admirable goal, and I hope he succeeds (though I would prefer that he focus on releasing a Windows Phone app, instead.  Ah well...).

In order to stay motivated (and because it's cool) I have decided to track my runs and chart my progress throughout the year.  Excel works just fine for this, but I want to try something a little different.  Why not use this as an opportunity to use FSharpChart? Continue reading

A puzzle

A starship is travelling at constant velocity through discrete, 1-dimensional space.  Its velocity is known to be some integral number of units per second.  You don't know the ship's position or the magnitude of its velocity ahead of time.

Your goal is to destroy the ship by hitting it with a photon torpedo.  You can detonate one torpedo per second, instantly, at any single point along the line of space.  Any particular point may be targeted as many or as few times as you please.  Your supply of torpedoes is unlimited.  There is no feedback from misses, but you do know when you hit the ship.  Assume time is discrete and aligned between you and the ship.

Devise an algorithm for firing the torpedoes that is guaranteed to hit the ship in some finite amount of time.  That is, if the ship's location and velocity details at some point in time were to be  revealed, you could calculate the exact number of seconds it would take for your algorithm to find the ship. Continue reading