Friday, May 30, 2008

Erlang is interesting

Late last summer, I picked up Joe Armstrong's Programming Erlang, after having gone from "having heard the name" to hearing the tub thumped -- even if that thumping offered faint praise in its title. But that alone made it the most obvious functional language to try out, as another paradigm shift after having done Python and Ruby.

So I took the book as holiday reading for while cycling through Suffolk (pdf sample, previous edition). And didn't do anything with it after.

Partly this was because work was going into crunch mode, where it stayed up to a couple of weeks ago, partly because I have enough other items in my hobbyist queue, and even then, I mainly do stuff I want to use, which tends to mean local apps.

During the latter part of the autumn, one of the unplanned bits of work I ended up doing at short notice was putting together a simple protocol terminating proxy, for circumstances when the pre-existing Apache-based solution was just too heavy-weight; and for other reasons, a .Net based approach had been ruled out. So what I ended up doing could be termed classic code-reuse, fitting together existing bits of 'C' and C++ code, and tweaking to fit (“And here is one I prepared earlier…”).

But all the time I was doing it, I was thinking that a piece of concurrent, high-reliability code would be so much easier done in a language made for the purpose. For one thing, it would not require spending a good 2/3 of the time just appeasing the necessary Windows APIs; and for another, a functional approach would simplify the handling of the protocol framing and parsing required.

But it worked (after the initial round of silly bugs got cleared out), and proved more robust and capable than I had dared hope. And finally it shipped.

Still, I wasn't satisfied. It could have been better.

Taking up a backlog of self-directed training time, I started from the ground up in Erlang, and as my very first piece of coding in the language (beyond 1+2 -> 3 at the command-line sort of things), put together in about a day and a half, the easy 50% of the proxy -- something that had taken a week when fitting together pre-existing code in a language that I consider myself fluent with, and using familiar APIs.

What's more, it even behaves better under stress tests -- Erlang's {active, once} mode for receiving sockets makes the business of traffic throttling simple a one-liner, so flow is smoothed out and not bursty (better behaved, even, than an equivalent proof-of-concept C# implementation using asynchronous reads).

While it might not be the language for file I/O with big files, for network I/O intensive proxying of a single connection, it does just fine.

The interesting thing about writing this app -- about 200 lines total -- is that the language makes conventional coding idioms look ugly. It looks much nicer to break out that nested if clause into a separate function, where part of the work is done in the function's argument list -- even if it only gets called from one place.

And the application is one which does play to Erlang's strengths beyond the functional nature -- lightweight processes, immutable values, message passing, dynamic typing. From the little I've done so far with F#, it would be harder work (for me at least) to manage the same power/line of code or per man-hour.

Sunday, May 25, 2008

Manga — JoJo's Bizarre Adventure : Battle Tendency

Fifty years have passed since Jonathan Joestar fought the Stone Mask Vampire, Dio Brando. Now his grandson Joseph comes to New York, following news of his grandfather's associates. This leads him into adventures in Mexico, Rome and the Alps, fighting the creatures that prey upon vampires — the Pillar Men, Santana, Wham, AC/DC and Cars. And because it's 1938, there are of course Nazis.

This is more of the pulp adventure Boys' Own stuff that we saw in the previous story, with the same gusto and even more outrageous super powered brawling, underground catacombs, Nazi secret bases, car chases, chariot races, damsels in distress, villains who won't stay dead,… Everything and the kitchen sink, really.

Indiana Jones, eat your heart out!

Anime — Spring Season '08

The winter season this year was a wash; mostly moe-moe harem shows. Of the ones I looked at, Shigofumi and Spice and Wolf lost my interest after a couple of episodes each, while Hakaba Kitarou has suffered from erratic subbing, and is still not complete.

What a contrast spring season has been! Now past the half-way point, time for a state of play report.

First, a little filler show, two and a half minutes, four mornings a week, Chi's Sweet Home is a slice of life about a lost kitten and the family who take her in. Acutely observed, this is a hit with anyone who's ever shared their space with a kitten.

The more conventional shows, top tier. From top left, clockwise:

Real Drive, the new Shirow/Production IG series, a return to metaphysical cyberspace in the 2050s. With only 4 episodes subbed, it's not clear where the plot is going; but the character designs -- as evinced by the gynoid in the picture -- are a change from the too typical anorexic stick figures, giving the show the nickname of Healthy Drive in some quarters.

Kaiba, from the same director as Kemonozume, a tale of a world where a human identity fits on a hot-swappable plug the size of a fingertip, and memories have become commodities to trade. The protagonist wakes without memory, just a locket with a photograph of a girl, and is trying to recover what he has lost. Again, subbing is slow.

Nijuu-mensou no musume, The daughter of Twenty-faces or Chiko, Heiress of the Phantom Thief depending on whether the group has taken the Japanese title, the literal translation, or what turns out to the the official Anglicization. Twenty-faces is a Robin-Hood type pulp thief drawn from existing Japanese interwar pulp fiction, but re-located into what looks more like a 1950s timeframe here; and the eponymous Chiko is a girl he rescued during one of his exploits. Six episodes in, and the plot is kicking in “Just as planned!”…

Allison & Lilia, based on an earlier series of light novels by the author of Kino's Journey, is possibly the slightest of this group. An alternate world, 1930s airplanes, and Adventure! with a sort of Tin-tin-esque vibe to it.

The watchable tosh, same order

Wagaya no Oinari-sama. In order to protect the youngest scion of the Miduchi family from spectral forces, a fox spirit released from decades of bondage, and the magically adept young Sentinel of the clan are assigned to guard him. Only they know nothing of modern life, the Sentinel is a classic anime disaster area in the kitchen, and the older brother is forced to try and police this mess. Cheesy but fun.

Golgo 13 is the classic Japanese hit man. He shoots, he scores (especially with the ladies) -- while remaining stone faced throughout.

Zettai Karen Children is pretty much standard kids cartoon fare about three psychically talented girls being used as superheroes by a government agency -- were it not for the fact the red-head acts like she has the mind of a dirty old man. At which point it is clearly aimed at teenagers of all ages.

Himitsu : ~Top Secret~ -- solving murders by reading the brains of the victims, a team led by a troubled bishonen have to deal with the ethical impacts of knowing all these inner secrets. Gay undertones abound, though the main character seems to be more interested in his own sister... Lol, Japan! Also, most likely to be dropped.

Wednesday, May 21, 2008

All change

This morning I was thinking about wouldn't it be nice if the car would sometime be gotten rid of, that had been parked, decaying and now overgrown, on next door's drive since the old lady living there had a stroke, what ten or more years ago.

Tonight when I got home from the Beer festival, it wasn't there.

And, thinking about it, I don't recall having seen the regular nurses coming in for the last couple of days. I don't think she lives there any more.

Later -- a skip arrived Friday, and the sons-in-law were filling it up on Saturday. It does look like a clearance.

Friday, May 16, 2008

When does x/2 != x>>1 for integer x?

As I discovered, when porting the Ruby big-integer unit tests to C# to test a BigInteger class --

Answer: Sometimes but not always, when x is negative and odd.

Python and Ruby -- native and DLR -- do maintain the identity:

IronPython 1.1.1 (1.1.1) on .NET 2.0.50727.1433
Copyright (c) Microsoft Corporation. All rights reserved.
>>> i = -57
>>> a = i/2
>>> b = i>>1
>>> print a,b
-29 -29

version 2.0β2, IronRuby pre-alpha, Python 2.5.2, Ruby 1.8.6 equivalents all omitted.

Indeed the Ruby big-integer unit tests contain a couple of tests explicitly to assert this identity. But doing

gives
/2 -> -28; >>1 -> -29

following the 'C' family "round towards zero" approach.

This is a potential peril in polyglot programming within an application -- the "same" arithmetic expression can yield different results depending on the language being used locally. Just being on the CLR, for example, does not mandate your arithmetic -- the language definition does. This is not something that one would at first consider might happen.

Thursday, May 15, 2008

Links for 15-May

Updated -- Browser event compatibility tables.

.Net 3.5 SP1 beta changes (StrongName bypass, LocalIntranet becomes FullTrust) default behaviours.

NullObject.For -- dynamic "do-nothing" mock for C#, automates a very common case.

TK-UI CSS Engine -- CSS styling for Java GUIs

Sunday, May 11, 2008

Installing Specsharp (Spec#) on Vista

The Spec# installer gives an error code of 2869 when installing on Vista with UAC active. A little prodding around the 'net led me to this blog comment which gives javascript source for an installer fixer-upper that can be run over the SpecSharp installer and then it runs perfectly happily.

Anime — Wevangeliwon 1.01 : You are (not) alone

The original TV series was one that I had heard about as required viewing, one of the must-watch classics of Japanese giant robot anime, long before it was available on DVD and before I had the technology on tap to get sucked into the anime/fansub scene. As such it was the second thing I picked up, after Dirty Pair Flash, and it sort of became a way of life for a while.

I haven't blogged a real review of the original series, because I put together a fan site instead. As a series that was director Hideaki Anno's art-therapy for depression, it is full of characters who are themselves psychologically damaged in one fashion or another. If you see yourself in any of them it can be very raw experience -- for me, it was Asuka, aspects of whose plight touched upon areas I had thought were thirty years exorcised and buried -- and hence the whole series becomes something of a mirror to the viewer.

Now, over a decade later, a remake in movie form, with the first part covering 6 out of 26 TV episodes in about 2/3 the time.

It clearly shows that this is done with more money and technology (as the shots of Ramiel, the no longer just octahedral angel, show), but less time -- the school sequences are compressed. But while some things stay the same -- Misato's photo -- others change, for reasons yet to be revealed, if for any reason beyond "it looks cool" -- the streets where Shinji sees the apparition of Rei are now years overgrown, rather than just-now evacuated; and the sea that has flooded parts of Japan is now red.

One of the lures of the original series is that it told a lot of the tale by implication -- you have to piece the bigger picture together from what the characters say to each other, or just how they react. After just retelling the first quarter or so of the tale, the movie ends with a big reveal that came near the end of the series, and then a whole bunch of entirely new material -- including a preview of the next movie that is 80% entirely new material inconsistent with the series (and more akin to bad fan-fics -- who is this Mari, the new girl in glasses, anyway?).

Oh yea, that title "Wevangeliwon" -- well, the kana used in the movie title to spell the foreign word have changed, dragging in two obsolete characters normally romanized as "we" and "wo".

Of course it's a must-watch, but more because of the original than from its own merits.

A happy day

Yesterday, in the blazing sun, I drove down to Basingstoke for the wedding reception for Nick, my team leader.

Given the weather, I decided to go the pretty way down -- the normal cross-country to Aylesbury, then A-roads as suggested by the AA route map, which I reckoned would take about 3 hours, and did (plus time spent filling up before really setting out). It was also hot enough that after I filled up, I took my shirt off before it got sweat soaked and drove the bulk of the way bare chested.

The heart of England in late spring sunshine always looks gorgeous, though I was surprised that reception for digital radio was lousy on the stretch between Prince's Risborough and Reading.

After the usual bit of snatching glances at the route instructions and the "which right turn is it" terminal guidance, I got there at a decorous time, in an interval in the Morris dancing.

Though it was a case of only knowing the people there from work, there was a vicarious sort of feeling of it being like being at a little brother's wedding do.

The cake was a 3-tier sponge, covered with rice-paper flowers (that made it look barnacle encrusted to prosaic-minded me), that wobbled perilously when being transported. Inside the decoration it was very sticky icing, and tiers of lemon layered with thick lemon cream, and chocolate.

Being there solo, and needing to get back to be care nurse for the morning, I made my excuses when the food was over and it was starting to get to the loud music and drink stage, coming up to 10pm. The home run I did the M3-M25-A1 way, not much shorter than the pretty way, but even cruising at a fuel efficient ~60mph most of the way, the traffic was light enough let me keep an average of almost 50mph. And fuel efficient it was -- the first pip of 5 on the fuel gauge emptied 160 miles after filling up (and I usually reckon about 90 in my usual commuting pattern).

And now he's off for a fortnight in Italy, I have to go back to work tomorrow.

Thursday, May 08, 2008

A cautious hope

I now have two new crowns fitted -- a gold one holding together the cracked tooth where the gum has been healing for the past 18 months; and a porcelain one that could be fitted onto the remaining bits of the decapitated tooth, even if it is set a bit low.

The former took a lot of wedging to get into place for the test fit, the neighbouring teeth having closed in on the gaps that had been widened to allow a crown, and a lot of work to prise free again so all the glue could be applied. I can feel it still as being tight against the other teeth, but hopefully that feeling of teeth in bandage should fade in a few days as the teeth jostle about.

The other went in easy, and we hope that the lashings of glue will hold -- otherwise it's a root canal on a live tooth, and fitting a post that will be needed. *ugh*

Wednesday, May 07, 2008

Links for 7-May

Cross platform standalone Silverlight applications.

Error reporting in Silverlight for Ruby and Python, and why they are as they are in Silverlight 2.

Sneaking Ruby 1.9 into the Google App Engine -- _why is a cunning fellow.

And IronPython 2 is now at beta 2.

Unit testing -- testing the right things.

Unit testing is good (as I have blogged before); with a test framework, it means you can test just a little bit of your code without having to set up an entire environment (if it even exists as yet!), run through complicated manual processes, before ever getting near the code you want to exercise.

The trouble is that tests can so easily miss the point of what it is you are testing.

I am a great believer in the style of design by contract -- objects interact with the world through an interface, which defines a set of stimuli to which the object can respond and what they should do in terms of success or failure (failure possibly being non-deterministic (e.g. out of memory, I/O failure...) for each stimulus.  Tests should be written to validate the contract, and be oblivious to the implementation.  If an implementation change is made -- such as to enhance performance -- then the tests should not fail.  Coverage percentage may well change, but if the contract is still followed, and the test tests to the contract, it should still pass.

Where an object collaborates with other objects, it does so via their interfaces (in .Net terms, public, or public plus internal methods according to context).  To prise an object free from the whole application context, then it needs to be provided with dummy objects which provide it with the collaborative environment it which it will work.

This is the problem with some well known mocking frameworks -- they do not facilitate implementation of the collaborator's interface in general form, as a proper test should.  This can rapidly get you into a way of thinking about test writing that tests implementation and not interface.  The NMock cheat-sheet is an example of what you should not do -- to take one particular example

Possible Method Call Expectations
Expect.Once                            Expect.Never               Expect.AtLeastOnce
Expect.AtLeast(<# times>) Expect.AtMost(<# times>) Expect.Exactly(<# times>)
Expect.Between(<# times>, <# times>)

It should not matter to a functional test how many times a collaborating object is invoked or through which methods; the important thing is that the mocked collaborating object should respond according to the contract of its own interface, and that the test validates that the object under test honours its own interface definition. (The RhinoMocks record/replay approach is equally guilty in this respect).

I could do some processing on a block of 20 bytes from a stream of data by reading 2 bytes ten times, 20 bytes in a chunk, or reading up to 1024 bytes and pushing back any surplus onto the stream (assuming the stream contract supports an un-read/seek type behaviour).  The unit test should only provide the stream with to be read, charged with an appropriate number of bytes (depending on whether the test is testing a positive case or a case of insufficient or superfluous data) and not constrain the process any further.  Indeed, the number 20 may even be an implementation detail, and the real issue is that a whole semantic unit on the wire be read and no more. (See the unit testing for my refurbishment of the .Net/Erlang bridge for a starter.)

Where developing -- designing -- for testability does make changes is the need to write loosely coupled code.  Where you don't need to refer to concrete types, only their interfaces, then that is what you should do, so that it is easy to replace inputs with appropriate mocks. To this end, constructor use -- rather than factory calls -- should be considered (people have also suggested that public constructors are evil for other reasons than just testing, too.)

Tuesday, May 06, 2008

Twilight

A chance to rediscover how pleasant spring twilight can be. Refreshing cool, after the warmth of the day, and with the east wind died down (which had made if a bit of an effort for cycling home).

A very thin crescent moon visible, with increasing contrast, between 21:05 and 21;25, before getting lost in the horizon clutter.

Monday, May 05, 2008

Bank Holiday surprise

A surprisingly bright and sunny day for a Bank Holiday, not what had been being forecast.

So, out on the bike -- and doing an impromptu pub crawl through north Essex, with a pint of Tanner's Jack at the Ickleton Lion, an IPA in the Axe at Saffron Walden, and Spitfire, with a background of Dixieland Jazz at the Rose and Crown in Ashdon, before doing a big loop around Cambridge and home.

Thursday, May 01, 2008

AARRGHH!!!

The temporary crown in the lower right -- and some of the plastic stump -- just came off!The whole built up stump just came off -- carrying the temporary crown with it! Not only is this an inconvenience, it also means that means the mould taken last week is useless.