Passed my microsoft exam

June 9th, 2009

I’ve been studying for my Microsoft certification for the last four days, and this afternoon I passed. So I am now certified in the world’s most boring subject.

Make a cafe au lait in the office

February 3rd, 2009

I’ve just gone to the nearby coffee bar for a lunchtime latte, and I was bitterly disappointed. Filth! So I won’t be buying latte there any more.

Instead, I’ll be making cafe au lait in the office. Cafe au lait isn’t just white coffee; it’s a 50-50 mix of hot milk and double-strength coffee, and bears a remarkable resemblance to a caffe latte, just without the foam cap. It’s much richer and more luxurious than a white coffee.

If if you have fresh coffee and fresh milk, you can make a very passable cafe au lait in minutes. Here’s the skinny.

Equipment:

  • a cafetiere.
  • a microwave.
  • a large mug or microwave-safe jug, to heat the milk.
  • a smaller mug, to serve.

Ingredients:

  • milk.
  • coffee ground for cafetiere.

Method:

The steps are; start the coffee brewing, heat the milk, plunge the coffee, then mix.

Start the coffee brewing: To make the coffee, boil a kettle of water and either let it cool for a minute, or put some cold water. Boiling water can scald the coffee. I know this sounds precious, but it makes a difference; you won’t taste as much bitterness in the coffee, and you should notice more of the flavours.

Now you’re going to make double-strength coffee. I use two heaped desert spoons in a cafetiere that will make three cups (two mugs) of coffee, but then I half-fill the cafetiere. Give it about three minutes to brew. During those three minutes, we’ll heat the milk.

Heat the milk: Take your large mug and half-fill it with milk. Nuke it for about a minute and a half.

Plunge and mix: When that’s done, your coffee should be about ready. Plunge the cafetiere, then mix the milk and coffee about 50-50. You will end up with something that looks like white coffee, with some wisps of white almost-foam floating on the surface. You’re done!

Drink in small amounts; it’s a rich drink and you don’t need huge quantities. Save yourself from bad industrial estate caffe latte!

Python-style string formatting for C#

January 24th, 2009

Jon Skeet recently asked in one of his posts:

it would be really nice to be able to write:

throw new IOException("Expected to read {0} bytes but only {1} were available", requiredSize, bytesRead);

Which would do the same as

throw new IOException(String.Format(
    "Expected to read {0} bytes but only {1} were available", 
    requiredSize, bytesRead)); 

And it got me wondering about the String.Format method, and how much uglier it makes C# code to read than, say, the equivalent python code. Alongside each other;

// C#
string message = String.Format(
    "Expected to read {0} bytes but only {1} were available", 
    requiredSize, bytesRead);

// python
message = "Expected to read %s bytes but only %s were available" % (requiredSize, bytesRead)

I think I’d solve the problem, not by creating a new constructor for IOException, but by making String.Format part of the C# syntax. It works very nicely for python, and it’s such a common thing to do that I tink it would warrant a change to the language. Given how cumbersome String.Format is, it’s often shorter and clearer to use simple string concatenation. This makes things rather inconsistent.

Here’s what I came up with. It’s a ‘first draft’, and more for interest’s sake than as something I’d put into production.

Instead of passing an object array in as the values, I’m reading from the properties of an object. So you can do it with objects or tuples;

var person = new Person() 
{ 
  firstname="Steve", 
  secondname="Cooper" 
};

Then you can inject the tuple into a format string like this;

string message = "{firstname} {surname} says injecting properties is fun!".ㄍ(person)
// message == "Steve Cooper says injecting properties is fun!"

So you’ll see this weird thing on the end of the format string that looks like a double-chevron. This is supposed to look like a double arrow, pushing values into the format string. In fact, it’s the Bopomopho letter ‘G’ and therefore a perfectly normal C# method name.

Here’s the code for the double-chevron method. I say again, this is just a proof of concept, not production code. Use at your own peril. (In fact, don’t use. Write your own. It’ll be more solid.)

public static class StringFormatting
{
  public static string ㄍ(this string format, object o)
  {
    var rx = new System.Text.RegularExpressions.Regex(@"\{(?<name>\w+)\}");
    var match = rx.Match(format);
    while (match.Success)
    {
      string name = match.Groups["name"].Value;
      format = format
      .Replace("{", "{{")
      .Replace("}", "}}")
      ;
      format = format.Replace("{{" + name + "}}", "{0}");

      object prop = o.GetType().GetProperty(name).GetValue(o, null);
      format = string.Format(format, prop);
      match = rx.Match(format);

    }
    return format;
  }
}

SonicFileFinder for Visual Studio

December 16th, 2008

For those of you who use Visual Studio all day, can I suggest that you install SonicFileFinder?

This lovely little addin by Jens Schaller gives you a way to find files in your current solution with a few keypresses. Invoke it, and you see a dialogue like this;

Sonic File Finder

Type in a fragment of a filename, and you’ll get a filtered list of files matching that fragment. Choose a file, hit ‘return’, and the file opens in the code editor.

Basically, if you know the name of your file, you no longer need to use the Solution Explorer. As codebases get bigger and bigger, this addin gets more valuable as the Solution Explorer gets worse.

Highly recommended, plus it now works with F#, C#, and VB.NET projects.

Twittering

December 1st, 2008

Now twittering on http://twitter.com/stevecooperorg. Feel free to follow. If I can tell who the hell you are, I’ll follow you back.

FxCop rules for functional programming

October 20th, 2008

Languages like C# 3 have come on great leaps and bounds when it comes to allowing a functional programming constructs. However, they are at heart imperative languages. I wonder whether you could use FxCop/Code Analysis rules to try to bully yourself into correct behaviours, like using immutable variables.

I have no idea how to go about doing this — particularly, I don’t really understand what the rules should be — but I think it would be interesting to develop a C# project with a set of rules promoting a functional style; it might be easier than, say, learning F#.

Using speech synthesis to improve your writing

September 24th, 2008

I’ve just discovered a fancy-pants new way of helping me with my fiction, and I thought I’d share. The idea is to use a speech synthesiser to listen to parts of your writing. By listening, you hear problems that aren’t so obvious on the page.

I recently bought a copy of Cepstral’s text-to-speech software, buying one of their voices for $30. This lets you do some very nice things. Originally I just intended to listen to web pages using the Click, Speak firefox extension. Rather nice; visit a web page, choose ’speak selection’ from a menu, and hear the web page. Neato.

But I’ve found it more useful when editing fiction. Select a sentence or paragraph and have it spoken back to you. As you listen, awkward phrases will jump out at you. Sentences that flow badly become more obvious. Good writing seems satisfying when you hear it read out.

So how can you go about it? Well, Cepstral do a free demo download of their voices. I’m using Cepstral Alison. Cepstral do their own text editor called swifttalker. Just cut and pastie text into the editor and click the play button. Until you buy it comes with a prefixed message about licenced, but if you purchase it’ll go away.

If you are more technically inclined, and are comfortable writing scripts for your word processor (say, macros for MS Word, or python for Sublime Text, or lisp programs for emacs) then you can use the swift.exe program that comes with a Cepstral voice, piping selected text into the executable. That’s what I’m doing, and it’s great.

Ira Glass on storytelling

July 8th, 2008

A great series of short videos on storytelling.

On the basics;

On finidng great stories;

On good taste;

On two common pitfalls;

podcast recommendation; The Intercontinental

June 20th, 2008

Pop yourself over to The Intercontinental to hear an amazing mix of music from around the world.

This isn’t ‘World Music,’ not some terrible folk music. It’s a vast array of different styles from around the world — hip hop, prog rock, surf rock, whatever — stuff you don’t usually hear just because it’s not in your language. The playlists mention;

  • Moroccan hip hop
  • North African, international flutes and more Cambodian music
  • French Psychedelic
  • Ethiopian synths
  • Burkina Electric
  • Korean psych guitar
  • Yugoslavian Mariachi (Meksikanski)

Anyway, it’s a weekly show, about two hours, and it’s available as a feed. Download it, love it.

My story, “No Tomorrows,” now available on Pseudopod

June 20th, 2008

Dear all,

Can I invite you to listen to a story?

A short while ago, I sold my horror story, No Tomorrows, to Pseudopod. Pseudopod, for those of you who don’t know, is the world’s most popular horror podcast. A podcast, for those of you who don’t know, is an audio magazine dishing up weekly MP3s that you can listen to on your iPod or PC.

Anyway, it went live today, and is narrated by the rather Excellent Alasdair Stuart. It clocks in at about 24 minutes.

There are several ways you can get the story;

Anyway, enjoy!