Phillip Trelford's Array

POKE 36879,255

Turing drawings

A little while back I stumbled upon a fun generative art project by Maxime Chevalier called Turing drawings that produces 2D procedural art based on randomly generated Turing machines. The project’s code is in JavaScript using a HTML canvas which you can try online, just hit the random button to generate all sorts of weird and wonderful pieces. The project has inspired amongst other things a port to asm.js and a version with genetic recombination of URLs.

For fun I’ve created an F# script that generates Turing drawings that can be run online below via the embedded Cloud Tsunami IDE:


The Tsunami IDE also runs on the desktop and can be embedded into applications like Excel.

If you’re interested in learning more about generative art and trying similar ideas then why not pop down to Skills Matter on Thursday August 15th for a hands on coding session.

Prototype

JavaScript is good, so as part of JavaScript the Good Parts, prototypal inheritance must be really, really good.

JS The Good Parts

Some time ago while I was working on a supply chain management system for a large UK based department store I came across some interesting C# code, here’s an example:

public class Customer : Address 
{ 
}

Clarence

Unfortunately Clarence above has no address, but thanks to Homeless Hotspots he is a Mac address. A stark warning of the implications of favouring inheritance over composition.

When asked the C# programmer said that they preferred inheritance as it was quicker to implement in C# than composition. For a single object it is easier to favour inheritance over composition in C#, even more so with C# 2.0 before the advent of auto-implemented properties. Here’s the somewhat more verbose Customer has an Address:

public class Customer
{
    private readonly Address _address = new Address();

    public Address Address 
    { 
        get 
        { 
            return _address; 
        } 
    }
}

The Gang of Four in their seminal Design Patterns book postulated in the first chapter that good object-orientated design should favour composition over inheritance. Unfortunately most people seem to skip the first chapter and dive head first into specific patterns like singleton, proxy and factory. Spring’s AbstractSingletonProxyFactoryBean shows the way:

i-dontalways-make-factory-beans-but-when-i-do-i-make-abstractsingletonproxyfactorybe

Arbitrary misuse of inheritance is not confined to enterprise developers, we only need to look at the Button class in WPF to see a long hierarchy and over 160 members.

Button Inheritance

Thankfully the Intellisense in Visual Studio 2012 has an intelligent search to help you find the needle in the haystack. In the future it may be possible to apply machine learning approaches for prediction so that we can scale from hundreds to thousands of members.

Prototypal Inheritance

Programmers often find JavaScript inheritance hard to understand. This is probably a good thing, creating a pit of success where composition is by default favoured over inheritance.

To this end I have implemented Prototypal Inheritance for C#. The source is available on BitBucket and I will publish a Nuget package for enterprise developers in the near future.

Prototype-based programming is actually quite a natural fit for C# particularly with the dynamic support introduced in C# 4, combined with the var keyword introduced in C# 3, it’s often hard to distinguish C# from JavaScript. 

Now for the science: in prototypal inheritance every object is a clone of another object.

clones


Pseudoclassical

Let’s start with the pseudoclassical JavaScript example from Douglas Crockford’s book:

var Mammal = function (name) { this.name = name; };
Mammal.prototype.get_name = function () { return this.name; };
Mammal.prototype.says = function () { return this.saying || ''; };
var myMammal = new Mammal('Herb the Mammal');
var name = myMammal.get_name(); // 'Herb the Mammal'

var Cat = function (name) {
    this.name = name;
    this.saying = 'meow';
};

Cat.prototype = new Mammal();

Cat.prototype.get_name = function () {
    return this.says() + ' ' + this.name + ' ' + this.says();
};

var myCat = new Cat('Henrietta');
var says = myCat.says(); // 'meow'
var name = myCat.get_name();

You can’t beat a good taxonomy of the animal kingdom to show the awesome power of OO. Now you can create almost exactly the same code in C# using the Prototype library:

var Mammal = Prototype.Constructor<string>((that, name_) => that.Name = name_);
Mammal.Prototype.GetName = (System.Func<dynamic, string>)(that => that.Name);
Mammal.Prototype.Says = (System.Action<dynamic>)(that =>
{
    string saying = that.Saying;
    Echo(saying);
});
var myMammal = Mammal.New("Herb the Mammal");
string herb = myMammal.GetName();
Echo(herb);

var Cat = Prototype.Constructor<string>((that, name_) =>
{
    that.Name = name_;
    that.Saying = "meow";
});

Cat.Prototype = Mammal.New();

var myCat = Cat.New("Henrietta");

string name = myCat.GetName();
Echo(name);
 
Henrietta

Prototypal

Again starting with a JavaScript prototypal example:

var myMammal = {
    name: 'Herb the Mammal',
    get_name: function () {
        return this.name;
    },
    says: function () {
        return this.saying || '';
    }
};

Now in C# taking advantage of anonymous objects:

var Class = Prototype.FromObject(new
{
    Name = "Hello",
    GetName = (System.Func<dynamic, string>)(that => that.Name)
});
dynamic myMammal = Class.New();

There it is, prototypal inheritance for C#, you’re welcome!

Connect All The Things – MVP Summit 2013

On Tuesday I did a lightning talk at the MVP Summit in Bellevue on connecting to all things, from statistics to statistical programming languages with Intellisense and LINQ using F# Type Providers, a feature shipped with Visual Studio 2012:


 

Connecting to an Excel file:

Excel Provider

Querying users stored on SQL Azure:

query {
  for u in db.Users do
  where (u.UserId <> userId)
  select u
}

Querying genres via the Netflix API:

query {
    for g in ctx.Genres do
    where (g.Name = genre)
    for t in g.Titles do
    where (t.ReleaseYear ?<= 1959)
    where (t.ReleaseYear ?>= 1934)
    sortByNullable t.AverageRating
    skip nToSkip
    take nToTake
    select t
}

Running B-Movie Sample on Azure: b-movies.azurewebsites.net

Connecting to the World Bank:

type WorldBank = WorldBankDataProvider<"World Development Indicators">
let data = WorldBank.GetDataContext()

let countries () = 
 [| data.Countries.India        
    data.Countries.``United Kingdom``
    data.Countries.``United States`` |]

Rendering charts asynchronously using JavaScript High Charts using TypeScript definitions:

let render () = async {
 let opts = h.HighchartsOptions() 
 opts.chart <- h.HighchartsChartOptions(renderTo = "chart", ``type`` = "line")
 opts.title <- h.HighchartsTitleOptions(text = "School enrollment")

Running in the browser using FunScript (F# to JavaScript compiler):

Charting World Bank

Querying Hadoop Hive in the browser:

query {
 for i in hive.iris do
 groupBy i.``class`` into g
 select (g.Key, Seq.length g) 
}

 

Iris data