Chef

Those who found themselves confused by my programming recipe the day before yesterday can now be a little less confused (hopefully): here’s the explanation.

The program is written in an esoteric programming language called Chef, who’s sole purpose is to be able to write computer programs that look like recipes. There are even competitions to write programs in it that can also be cooked as real dishes. It’s a strange world. Each of the ingredients is a primitive kind of variable (for the non-programmers: a named entity [eggs, penne pasta, etc.] that can contain a value). The values that these variables can be instantiated with are numbers, and the numbers are given at the start of the line. Therefore, at the start of the program, eggs=3 and penne pasta=56.

These are “put” into the mixing bowl one at a time. The mixing bowl is a stack – the things put in to it first appear at the bottom, with other things on top of it (for the non-programmers: stacks are immensely useful in almost all programming languages, so programmers tend to have no trouble with being told “the mixing bowl is a stack”). So, when the first item (penne pasta) is put into the mixing bowl, it (and it’s associated value, 76) sits at the bottom of the bowl, ready for other things to be “put” on top of it.

However, we do some more complicated things, such as “adding” a birds-eye chilli. “Adding” is not the same as “putting”. When something is “added” to the mixing bowl, the value of it’s ingredient is added, mathematically, to the value of the thing at the top of the bowl. So, for example, when we add “1” birds-eye chilli to the mixing bowl which contains only “76” grams of penne pasta, we end up with a mixing bowl containing just “77” grams of penne pasta.

“Combining” is another Chef operation. After we’ve put the fresh ginger (17) in, we combine it with the cinammon (5), which results in a multiplication of the top item in the bowl, resulting in 85. “Removing” the birds-eye chilli (1) reduces this number by 1, because the “remove” operation means “reduce the value of the ingredient from the value of the ingredient in the top of the mixing bowl”. Later, we “stir”, the mixture, which moves some of the values in the stack around (read the spec if you care). And eventually, we “liquefy” the contents of the mixing bowl, which turns the numbers into their Unicode equivilent (typically letters): N, A, M, T, A, C, and S.

The baking tray is the output buffer (buffet?) in which things must be placed to be output to the screen, and the “serves” directive indicates which baking tray (we only have one in my recipe, but the language specification allows for multiple bowls, trays, and even delegation of sauces and other side dishes to other chefs – see the recipe for Fibonacci Numbers with Caramel Sauce [and notice the recursion – “caramel sauce should be served with caramel sauce”]) we output to the screen.

Jon earns himself a pint from working through the program, the mad fool that he is. There was a flaw in his logic, though, that made him come up with “NAMTACS” as the answer: he forgot that the baking tray, too, is a stack, and that the order of the ingredients is not changed in the transfer from the mixing bowl to the baking tray (check the spec!)… when popping ingredients out of the baking tray, they come out in reverse order. A pint to Paul for spotting his mistake. The correct output is “SCATMAN”.

Thanks to David Morgan-Mar for this fantastic programming language. He’s also the man behind LenPEG, an image compression algorithm which, for selected images, can achieve lossless compression at a ratio of 6,291,456:1, and HQ9++, an object-oriented language which provides the most code-efficient possible method ever to write test programs such as Hello World, 99 Bottles Of Beer, and the Quine program, although nobody has ever successfully written a Fibonacci generator or a Towers of Hanoi solver in it.

A Challenge For My Programmer Friends

So you think you’re a dab hand at learning new and unusual programming languages: even the most bizarre of them. You can get your head around Perl, and you might have even looked at LISP. Well, let’s see who’s first to correctly tell me what the output of the following computer program is. It’s unique (I’ve just written it, and you won’t find it elsewhere on the ‘net), so you’ll have to first work out what the programming language is. At that point, you’ll need to either find a platform on which you can run it, or “whitebox” decipher it by hand.

A pint, and my respect, to the first person to solve it. If nobody solves it, the pint’ll go to whoever seemed to be most on the right track.

Scatman Dans Pasta Bake.

A quick and tasty meal for programmers everywhere: baked pasta with a spicy
kick. Cook and drain the pasta first, and pre-heat oven to 175 degrees Celcius
(gas mark 4).

Ingredients.
76 g penne pasta
75 g fusilli pasta
65 g grated cheddar cheese
64 ml vegetable stock
21 g courgette
17 g fresh ginger
11 g crushed garlic
8 teaspoons olive oil
7 g parsley
5 level teaspoons cinnamon
3 eggs
2 sliced new potatoes
1 birds eye chilli
1 pinch hot chilli powder

Cooking time: 30 minutes.

Method.
Put penne pasta into the mixing bowl. Add birds eye chilli. Add hot chilli
powder. Put crushed garlic into the mixing bowl. Combine parsley. Put fresh
ginger into the mixing bowl. Combine cinnamon. Remove birds eye chilli. Put eggs
into the mixing bowl. Combine courgette into the mixing bowl. Add sliced new
potatoes. Put vegetable stock into the mixing bowl. Add eggs. Put fusilli pasta
into the mixing bowl. Add olive oil. Put grated cheddar cheese into the mixing
bowl. Stir for 5 minutes. Liquify contents of the mixing bowl. Pour contents of
the mixing bowl into the baking dish.

Serves 1.

Warning: do not try to cook this dish as if it were a genuine recipe!

The Rails Cafe

This Rails Cafe article made me smile. Rails really is a lot of fun. If SmartData gets the upcoming project I hope it does, I’ll be pushing for Rails to be the development platform: the project would lend itself very well to it. Perhaps I’ll hammer out a hundred lines or so in advance, in order to demonstrate my point when the discussion comes. Again, assuming the client takes our quote.

I’m limping a lot better today, almost up to a normal walking pace, and my foot hurts a lot less (except when I walk for more than ten minutes on end).

Claire‘s made a great start on looking for a new place for us to live, finding a few possible places already and arranging for us to visit one this evening. This possible new place sounds fantastic, but might be a little beyond our budget. We’ll have to do the maths this evening.

Not Quite Where We Planned To Be

Claire and I are in Preston. Let me explain how this came about.

As I mentioned, we spent Friday night and most of Saturday in Gregynog, a beautiful stately home owned by the University of Wales and used as a conference venue. Every year, the Computer Science department ships almost the entirety of the second year out there to learn how better to get a job, in anticipation of hopefully getting an industry year placement the following year. Claire, as a department staff member, was invited along to help organise a group of students. I was invited along as an representative of the computer industry, there to give mock interviews to students of the kind that they might expect when applying for computer science related jobs for their industry year or for graduate positions.

It was a lot of fun. I met some interesting people and, with their help, got to grill students. Perhaps my favourite part was successfully catching out students who had… how shall we say it… exaggerated a little on their CVs. One fellow, I remember, had, while boasting about his web development proficiency, stated that he was familiar with HTTP. So I asked him what the fundamental differences between a GET and a POST method were. I’d have accepted something about request parameters being visible on the address bar, but no: no such luck. It was also good to be pleasantly surprised, such as by the database-proficient claimant I met who successfully, with a pause, disassembled the huge database relationship diagram I gave to him. My co-interviewer says I’m evil. I replied that I was merely thorough.

On Saturday night, in accordance with our plans, we continued on to Warrington to visit Gareth and Liz‘s new place. Gareth didn’t seem quite ‘with it’. But the food was good and I regretted eating so well at Gregynog that I couldn’t guzzle more, and the company was even better. After the party came to a quiet end, we dropped off Jimmy at his home in Runcorn, and decided to move on up to Preston to say “hi” to my folks.

Needless to say, my mum was at least a little surprised when Claire and I waltzed into her bedroom. We didn’t waltz, mind. More of a polka. But she was surprised, regardless. My dad returns from Vietnam today, so we’re hoping to catch him and have lunch before we return to Aber.

Paul: I bet, despite her trying to remind herself on several occasions, Claire’s still forgotten to call you to tell you that we’re unlikely to make the 2:30 screening of Howl’s Moving Castle at the Arts Centre, so I hope you read this before then.

Ah; Friday…

In the words of Bender, “…another pointless day where I accomplish nothing.” It’s feeling a little that way. Last night’s brownout knocked computers offline, and this morning BT severed a cable, depriving us of phones and internet access. It’s an hour and a half into the day already and I’ve got nothing done so far.

The worst bit is, having been told that the phones were down already: when I discovered that the outbound fibre optic link was down, I picked up the phone to call the facilities manager. Whoops! In an age when everything is connected and digitial, it’s easy to forget that things don’t always work as they should.

Add to this the fact that I’ll be leaving early today, and you’ve got a recipe for underproductivity. Which is a word, now, thank you very much. I’m off this evening to Gregynog, a University of Wales conference centre, as a guest interviewer, to give mock interviews to second-year Computer Science students. These interviews are supposed to be reflective of those given in the industry, in order to better prepare them for ‘real’ interviews and give them feedback… so I’ve been devising a list of some of the nastiest Comp. Sci.-centric interview questions ever devised. Here’s some of my favourites:

  • How would you explain a database to a 10-year-old child?
  • When choosing between programming languages with which to implement a specific project, what are the most important factors and why?
  • Pseudocode me an algorithm to detect whether two rectangles (with known top, left, bottom and right) overlap one another.
  • What is bytecode? Can it be reverse-engineered?
  • What is the sum of all numbers from 1 to 1000?
  • Do you make mistakes? Tell me about one of them.
  • Are you smart? Why do you think that?

I can’t write too many of the really nice fun ones here, because (a) there’s at least one person I’ll be interviewing who I know reads my weblog and (b) I’ve got some questions which depend on demonstratable code samples, which I can’t be arsed to upload anywhere.

Anyway; things look like they’re all booted up now, so I’d better get on with some work.

Duality

Duality, my main desktop PC at home, has been misbehaving, and I’ve had to take it to pieces, both virtually (tracing driver DLL calls – yum!) and physically (computer components littering the floor, etc.). It’s been two years since it’s last reformat/reinstall, which is a pretty long time for a Windows XP box treated the way I’ve treated it, so it’s definately time for a rebuild.

Shame I couldn’t have done it sooner/later, as it’s put a dent both in my NaNoWriMo writing and in my ability to investigate some code for a client I’m dealing with “on the side”. It’s going to be a busy little weekend.

In other news, I’ve been playing with Gosu, a sprite animation/drawing layer module for Ruby. Ruby’s execution speed (within an object-oriented paradigm) lends itself well to 2D animation and games. Here’s a Flash animation showing a recording of what I came up with:



Yes, that really is the sprite for Dan in the Dan & Alex comic.

If you’re really sad (and using Windows – I haven’t tested it under other operating systems and can’t be arsed packaging up the C-layer stuff for them, yet), you can actually have a play. Just install Ruby 1.8.2-15 For Windows (15MB) and the game itself (740K).

If I get really bored or suddenly find a lot more free time, I might actually finish making a game out of the engine I’ve put together.

Edit: The download link for the game has now been fixed. If you downloaded it and just got error messages when you tried to run it, try again now.

E-Mail Server Configuration For Dummies

Well; I feel like a wally.

SmartData‘s taking on a new client for a content-managed web site, web and e-mail hosting. In anticipation of the domain transfer going through without a hitch, I reconfigured our web and e-mail servers to already be ready to accept connections to their “new” web site and for e-mails to come through. This is a wise move, because not all of the computers on the internet appreciate moving domains at the same time, and so for a day or so, e-mails will be going to both the old e-mail server and the new one: the new one needs to be ready to catch these before the first computers start sending e-mail to it.

Unfortunatley, I underestimated the intelligence of the configuration tools and the anti-spam mechanisms of our e-mail server software.

So, I send an e-mail to our client, who we’ll call bob@our-new-client.com, from my @smartdata.co.uk address. It goes out through our mail server. This is where it all goes wrong. Normally what our mail server would do would be to look up where on the internet our-new-client.com’s e-mail is kept, connect to there, and deliver it, but our mail server is a little bit lazier than that. It thought to itself, “Hey, I am the e-mail server for our-new-client.com: I know this, because I’ve been configured to accept mail for them,” and so it happily filed all my mail to bob@our-new-client.com… on their new mail server.

And then the news came through that there were complications in the domain name transfer, and it would be a few more days before they could easily pick up said mail. So, I’ve happily e-mailed them a request for their deposit on their hosting package with us, and they’re wondering why we haven’t asked for it yet, so they e-mail us. Here’s what happens:

Their e-mail server contacts our e-mail server and says “I am our-new-client.com, and I have an e-mail for dans-real-address-goes-here@smartdata.co.uk.”
And our mail server thinks “Hang on, this server is pretending to be our-new-client.com, but I know that I am responsible for our-new-client.com’s e-mail. This must be a filthy spammer trick.”
But our mail server was configured by me, so it’s a little devious. It responds (to the real our-new-client.com mail server): “Okay, I believe you, give the the e-mail.” (so those filthy spammers have no idea whether their mail got through or not) Then it shreds the e-mail and buries it in virtual concrete.

And so, for the latter half of this week, neither our newest client or us have been able to e-mail one another, and it’s only today that I’ve noticed.

Bugger.

Black & White 2 Installer

I’ve just been installing Black & White 2, and was quite impressed by the following screenshot:

'everything.stuff' file

Guess nobody told Lionhead about using meaningful file names, eh?

Writing A Calendar App In Rails Vs. PHP

Some time ago, I wrote a web-based calendar application in PHP, one of my favourite programming languages. This tool would produce a HTML tabular calendar for a four week period, Monday to Sunday, in which the current date (or a user-specified date) fell in the second week (so you’re looking at this week, last week, and two weeks in the future). The user-specified date, for various reasons, would be provided as the number of seconds since the epoch (1970). In addition, the user must be able to flick forwards and backwards through the calendar, “shifting” by one or four weeks each time.

Part of this algorithm, of course, was responsible for finding the timestamp (seconds since the epoch) of the beginning of “a week last Monday”, GMT. It went something like this (pseudocode):

1. Get a handle on the beginning of "today" with [specified time] modulus [number of seconds in day]
2. Go back in time a week by deducting [number of seconds in day] multiplied by [number of days in week] (you can see I'm a real programmer, because I set "number of days in week" as a constant, in case it ever gets changed)
3. Find the previous Monday by determining what day of the week this date is on (clever functions in PHP do this for me), then take [number of seconds in day] multiplied by [number of days after Monday we are] from this to get "a week last Monday"
4. Jump forwards or backwards a number of weeks specified by the user, if necessary. Easy.
5. Of course, this isn't perfect, because this "shift backwards a week and a few days" might have put us in to "last month", in which case the calendar needs to know to deduct one month and add [number of days in last month]
6. And if we just went "back in time" beyond January, we also need to deduct a year and add 11 months. Joy.

So; not the nicest bit of code in the world.

I’ve recently been learning to program in Ruby On Rails. Ruby is a comparatively young language which has become quite popular in Japan but has only had reasonable amounts of Westernised documentation for the last four years or so. I started looking into it early this year after reading an article that compared it to Python. Rails is a web application development framework that sits on top of Ruby and promises to be “quick and structured”, becoming the “best of both worlds” between web engineering in PHP (quick and sloppy) and in Java (slow and structured). Ruby is a properly object-oriented language – even your literals are objects – and Rails takes full advantage of this.

For example, here’s my interpretation in Rails of the same bit of code as above:

@week_last_monday = 7.days.ago.gmtime.monday + params[:weeks].to_i.weeks

An explanation:

  • @week_last_monday is just a variable in which I’m keeping the result of my operation.
  • 7.days might fool you. Yes, what I’m doing there is instantiating an Integer (7, actually a Fixint, but who cares), then calling the “days” function on it, which returns me an instance of Time which represents 7 days of time.
  • Calling the ago method on my Time object, which returns me another Time object, this time one which is equal to Time.now (the time right now) minus the amount of Time I already had (7 days). Basically, I now have a handle on “7 days ago”.
  • The only thing PHP had up on me here is that it’s gmdate() function had ensured I already had my date/time in GMT; here, I have to explicitly call gmtime to do the same thing.
  • And then I simply call monday on my resulting Time object to get a handle on the beginning of the previous Monday. That simple. 24 characters of fun.
  • + params[:weeks].to_i.weeks simply increments (or decrements) the Time I have by a number of weeks specified by the user (params[:weeks] gets the number of weeks specified, to_i converts it to an integer, and weeks, like days, creates a Time object from this. In Ruby, object definitions can even override operators like +, -, <, >, etc., as if they were methods (because they are), and so the author of the Time class made it simple to perform arithmetic upon times and dates.

This was the very point at which I feel in love with Ruby on Rails.

Magic Story

Inspired by Andy‘s PassThePageGame on the RockMonkey wiki – a collaborative storytelling game based on that silly game you used to play at school, I’ve written my own tool:

MagicStory is a collaborative wiki storytelling tool designed to be harder to “run away” with than normal wikifiction. Having read the story, participants can write a suggested “next sentence”, and after three suggestions have been collected a fourth person will be able to choose which one is used. Give it a go!

You’ll need a RockMonkey account, of course, but they’re free and simple to set up.

Rock Exchange

This weekend I hope to release Rock Exchange, my latest WikiGame on the RockMonkey wiki. I’m just finishing performing some tests and fine-tuning on it now. Here’s a screenshot:

Rock Exchange game on the RockMonkey Wiki

In Rock Exchange, players will be able to invest their hard-earned Rocks (a unit of currency) in shares in the various pages on the RockMonkey wiki. The value of these shares will fluctuate based on several characteristics, and players will have to play the odds in order to know when to buy and when to sell to gain a return on their investment. A page’s shares fluctuate based on:

  1. Popularity: so if a new page seems likely to attract the attention of a lot of passing Googlers, it’s worth investing in, whereas an “in joke” that’s died out is not. Similarly, players can manipulate wiki page links in order to attract interest. The most valuable indicator of popularity is how many people come to the site for the first time, entering on a given page.
  2. Investment: investment in pages will put positive pressure on their value, but selling shares damages this value by flooding the market – who’ll be first to pull out of a high-flying page, thereby forcing other shareholders to suffer?
  3. Interference: there will be several methods for people to drastically interfere with the value of their shares and the shares of the other traders, but these won’t necessarily be immediately apparent.
  4. Random factors: the rock market, like the stock market, is an unpredictable animal, and there’ll be a small amount of luck in any investment.

The game’s pretty much “ready-to-play” in it’s most basic form right now, but I want to run it alone awhile longer and see if I can improve the balancing factors in it.

×

OpenID For WordPress

Update: 12th October 2007 – this project is to be considered abandoned. Please see How To Set Up OpenID For WordPress Comments instead. Thanks for the support and for your interest in OpenID.

THIS IS ALL HORRIBLY OUT OF DATE. THE DOWNLOAD LINKS DON’T WORK, I KNOW. GET OVER IT. More seriously now, I am working on a new version of this that actually works as a WordPress 2.0.x plugin. It’s very nice, but it’s not finished. Watch this space. In the meantime, why not take a look at OpenID Comments For WordPress (which is based on my preliminary work, here). Thanks for all the attention, guys.

As promised, I’m releasing the first usable version (v0.4) of my WordPress OpenID plugin tool. It’s very, very messy and a little buggy. Plus, installing it requires that you hack a few PHP files… use at your own risk. You’ll need a WordPress v1.5 weblog. Download this package and decompress it to your WordPress directory. It will create an openid_icons directory, a file called openid.php (the main codebase), and a file called openidform.php (the form that appears on your blog). Edit openid.php and substitute your own weblog URL in at the appropriate places (near the top). Link in the login form wherever you like. I’ve done so in my theme’s “sidebar.php” file, with the following code: <?php include (TEMPLATEPATH . '/openidform.php'); ?> In your main index.php, add a line to include the openid.php file. This will allow logins and logouts to be processed. Something like this: <?php require_once('openid.php'); ?> In wp-comments-post.php (the comments processor), substitute the following code in under “// If the user is logged in”: // If the user is logged in get_currentuserinfo(); if ( $user_ID ) { $comment_author = addslashes($user_identity); $comment_author_email = addslashes($user_email); $comment_author_url = addslashes($user_url); } elseif ($_SESSION['sess_openid_auth_code'] != "") { $comment_author = addslashes($_SESSION['sess_openid_auth_code']); $comment_author_email = "openid@example.com"; $comment_author_url = addslashes($_SESSION['sess_openid_auth']); } else { if ( get_option('comment_registration') ) die( __('Sorry, you must be logged in to post a comment.') ); } Notice the extra section, relying upon $_SESSION[‘sess_openid_auth_code’]. That’s the magic bit. And it should ‘just work’. Let me know if it doesn’t; I’ll be improving the codebase over the coming weeks and I’d like to include your suggestions. If you need any help setting it up, I can probably help with that too, or even with adapting the code to work with other applications (than WordPress). Features so far:

  • Authenticate OpenID users
  • Easily authenticate OpenID users from particular servers, including members of LiveJournal, DeadJournal, and Level9
  • Authenticated OpenID users can post comments

Features to come:

  • Cookie-based “remember me”
  • Ability to authenticate WordPress users (e.g. the weblog owner) by an OpenID
  • “Friends Only” protected posts, which can only be read by certain authenticated users
  • AJAX-powered log-in (to save users from having their browsers redirected excessively, and because it can be made to look swish), where supported

If you want to help code, just drop me a message.

OpenID And Scatmania

Over the last few weeks I’ve playing playing with an exciting new technology known as OpenID. Do you remember Microsoft Passport and it’s opposite number, Liberty Alliance? Well; we all know that these services weren’t all they cracked up to be. They claimed to be “distributed log-on services”, but in actual fact they were centralised log-on services (controlled, for example – in the case of Passport – by Microsoft – do you want Microsoft to know everything you do on the web?), and not really distributed at all…

…OpenID really is a distributed log-on service. Anybody can set up an OpenID server and start giving out OpenID accounts. If you have a weblog with LiveJournal, for example, you already have one, and soon folks on other similar blogging services will have them too.

I’d love to see a future where OpenID catches on, because it really is a beautiful and elegant (from a technical point of view) way of doing things, and it’s really easy to use from a user’s point of view, too. I’ve spent a little while implementing the beginnings of a WordPress (the blogging engine that powers this site) plug-in, and it’s taking shape: if you look in the upper-right of the page, you should find that you’re able to log in to this web site using your LiveJournal account. That means that WordPress users like myself, in future, should be able to do things like LiveJournal’s “friends only” posts, and allow LiveJournal users to make comments in a way that proves they are who they say they are, and many other benefits, too.

But, of course, it doesn’t stop there: DeadJournal will be next. Then TypePad. Then Blogger and the forum sites – phpBB and the like. Then the wiki sites. All of these sites will be able to authenticate against one another, and make content private, or accessible, without having to have silly “sign up” systems of the type we’re starting to see everywhere these days.

It’s all very exciting, but it’s early days for now. Right now, my WordPress plugin doesn’t do a lot – you can log in and out, and that’s about it. But give me a go, and tell me what you think – log in to my blog using your LiveJournal account, and give me some feedback. And when I finally get this code to a production level (right now it’s buggy as hell), I’ll release it as a WordPress plugin, and the world will be great.