Wednesday, October 29, 2008

Anime — Wagaya no Oinari-sama

Our Home's Fox Deity was one of the surprises of the spring season. The first episode, which introduces the main characters -- two brothers (Tooru and Noboru Takagami), the fox spirit that their family had sealed away (Kuugen Tenko), and Kou, the guardian priestess of the clan -- is all SERIOUS BUSINESS, with plenty of archaic and technical (for the purposes of magic) Japanese, until it at the close, when it looks like it's going to become a magical girlfriend series.

Next episode changes the mood again -- we meet the local deity where the Takagamis, and their new guests, live, and he's the manager of the local 7-11 equivalent. And so it goes -- while the original motivation, that Tooru is at risk from spectres, powers a few fight scenes over the first third of the series, that quite soon fades into the background. What becomes important is that with such houseguests, all the local supernatural goings on start to leak, in a comedic fashion, into their lives -- a neighbouring kami has lost an item of power, the recovery of which intrudes into their lives, as do a homeless family of fox-spirits, the local church run by oni, a werewolf, and the celestial agents trying to keep life tidy for everyone.

Meanwhile Kou and Kuu, both unfamiliar with modern life, provide a constant source of broad comedy -- and the appearance of rivals for Noboru's affections in the eyes of the girl who really fancies him. And in this way, 24 episodes pass, with each little episode tidied up, but no conclusion yet in sight.

I had been prepared to drop the series after a couple of episodes, when it hadn't properly gelled, but Karen was attracted to the whimsical style, so we kept on watching -- and I'm glad to have gotten over that initial hump. So, not a classic of the ages -- just something fun that doesn't take itself too seriously, and makes effective use of the creatures of Japanese folklore dragged into the modern mundane world of tight household budgets.

Tuesday, October 28, 2008

Links for 28-Oct

PDC link collection -- tons of Azure links, including tools; C#4 preview, and more.

typeface.js -- font embedding via <canvas> and text replacement.

Geneva -- the new codename for the identity framework formerly known as Zermatt.

Thursday, October 23, 2008

Anime — Strike Witches

I summarised this series before; but now at the conclusion I can say it was the hit of the new-in-Q3 titles.

All too often, anime series manage to lose the plot spending too much time setting up panty shots for reasons of fan-service, and not enough on the story. Strike Witches managed to avoid this common trap by leaving the girls half-dressed at best (though also including a number of steam obscured hot bath scenes for "uprating" in the DVD release).

By this ruse of continually running the fan-service in tandem with a setting that panders to the military otaku by including a wealth of period detail all over -- like making Sanya Litvyak's magical antennae (lower right) match those of the plane her prototype (Soviet ace Lydia Litvyak) flew; or naming Charlotte "Shirley" Yeager's plane after Chuck Yeager's Glamorous Glennis, to name but two -- Studio Gonzo have succeeded in making a series where the fan-service actually receded into the background. Once you've watched through the dogfighting in the first few minutes of the first episode, you get desensitised to the full screen pantsu.

The story is a fairly standard and slight one, but executed with style and humour, and just a little dash of "monster of the week" -- Yoshika Miyafuji is taken from the land of Fuso to Britannia partly in search of her father, but eventually to help protect against the scourge of the Neuroi, extradimensional invaders who have overrun much of mainland Europe. So there are friendships, rivalries, personal sorrows, and the inevitable brass who don't care for the unorthodox nature of the 501st Joint Fighter Wing, right up until the time in September 1944 when Gallia is liberated.

And then we end with a teaser for season two, which seems a given, the series having been an unexpected runaway hit (having apparently been pitched before last year's mecha musume title, Sky Girls, by the same designer, but left to later), an hit which may well have saved the studio financially.

Anime — Crystal Blaze

One of Q2s series that I picked up as harmless watching, and to that low standard it delivered (when subs eventually arrived).

It's a sorta-contemporary, sorta-Left Coast private investigator story -- a conspiracy with weird science (turning abducted girls into short-lived walking glass statues), the disenchanted ex-cop and a bunch of associated dead-beats and weirdos who get involved. Once you see the pieces laid out for you, you know how it's going to unfold, modulo the level of minor character deaths; and in that it does not disappoint.

Wins a Black Lagoon award for anti-helicopter tactics.

Sunday, October 19, 2008


Thursday morning just gone, stumbling downstairs in the dark at twenty to seven, to see the cats sitting at the patio doors, looking out, with the just-past-full moonlight streaming in.

A* in Scala -- would be a fairly mechanical translation of the F# version with probably more explicit type declarations. The F# inference of a numeric type for operator < and + fails, so those operations on generic type B have to be pulled into a Graph[A,B] trait.

A few apples still left on the Bramley, and a lot on the Charles Ross. And this lunchtime, a tortoiseshell butterfly and a fallen leaf lying side by side on the picnic table, looking almost the same at first glance.

Monday, October 13, 2008

A-star in F# Revisited

A light refactoring of the previous. It pulls in the worst offenders where functions really ought to have been nested; and passes the problem-specific parts around as a bunch of functions in a record type Graph<'a,'b>.

This lets me disentangle the generic graph traversal bit from the specific problem, something I was complaining about yesterday.

Updated to F# 11-Feb-10.

Since units of measure don't apply to int, I couldn't add a proper type discriminator between the metric for the graph and the node indices (let alone the set of swaps that are of type index XOR index in this representation) as integers. I could make the metric a float to distinguish it by type, so I did that instead.

The "no solution" cases that gave warnings have been provided for, and a few other minimal tweaks have gone into pattern matches. There are a few places where I'm still needlessly binding variables in patterns, but I think that is probably just a venal sin at worst :) -- I consider them to be little different from temporaries-for-clarity.

Sunday, October 12, 2008

Thrice is a charm — A-star in F#

The real goal of the last couple of exercises, creeping crabwise up on doing something in F# that's not totally trivial.

Having started with the wikipedia pseudo-code, Python was an obvious first real language; then to make it pure functional, Erlang was the obvious choice, before going to F#. And boy was that last step painful at times.

Really Erlang:Python :: F#:C# -- the first two being dynamically typed and forgiving; the other requiring dodges like nullable/option types for the "it didn't work" return cases. Those required some wrestling with the type system in the middle of the conversion from Erlang, though in the end, all the explicit argument type specifications did eventually go away.

The ability to nest functions is where the big changes is structure from the Erlang version show up. I could do a bit more of this -- pulling astar_step into astar, for example, or pulling compatible and equivalent_point into neighbour_nodes -- but I wouldn't want to pull too many long functions inside other ones, simply for clarity.

Also the generic engine could in all three cases be given function arguments to replace the coupling by name in these examples.

It's also annoying that calls always have to go up the source file -- smells a bit of 'C' in comparison with the customary freedom to refer back and forth as in the other implementations (or even in C# or Java) -- so that the algorithm bit is now in the middle and not at the top.

Yes, this does compile with a couple of warnings about incomplete matches in failure cases.

Monday, October 06, 2008

Heartfelt thanks for the Erlang tips!

This is what makes the Internet such a nice place. I'm working with a new language and environment because in a simple experiment, it showed promise for addressing a real problem at work, something involving concurrency and network connections i.e. the sort of arena Erlang was made to play in. My trouble then is, with no other in-house expertise, I can't get a code review for that which gives any great degree of cross-checking -- and I can't (obviously) put the real code out in the public arena for review.

By putting a separate piece of code -- nothing to do with the real work -- out on this blog, I have at least managed to get something of an arm's length review of style at least.

Having reflected on Vat's comment on the first post, I'm more minded to move the bit masking into the print_swap/2 function, to localise the concern (determining which side of the traverse one is going to) into one single scope, rather than spreading that process around two different functions, even though that loses the option to pattern match on a simple argument:-

The issue of localization-of-concerns wasn't such an issue with the Python version, since that was less aggressively broken into functions.

Sunday, October 05, 2008

A first refactoring of A-star in Erlang

Clearly in earlier (unpublished) attempts at using the case construct in Erlang, I had been making some very n00bish mistakes. Today with a set of code that worked, and could be modified step-wise, I find that when used properly, it does make things terser -- fewer intermediate variables, and without the true branch meaning "false" -- and then lets me pattern match on expressions that are functions of arguments, rather than only the arguments themselves.

The use of if for crossing_time and display gives a natural if/else if cascade (as the most "traditional" analogue for what the Erlang if does). Most of the time, using case has made the code terser (removing the need for most temporaries in simple binary choices, if nothing else -- here best_step is an exception, where the temporary looks to be a necessity); for update, it was about neutral. Maybe there is a trick I am missing, but anything going through a binary looks like it would make crossing_time and display more cumbersome, rather than less.

Once more with feeling… — A-star shortest path finder in Erlang

The same problem as in the previous post, only this time in Erlang:

Where I know that a recursion will be shallow, by construction, I've not attempted to write everything tail-recursively; that I've kept in the main A* algorithm, which should be generic enough to extract, and provide with extra function arguments.

The obvious effects of having to avoid state, with recursion rather than iteration, are more code and longer argument lists to the recursions. Still, an interesting exercise in rewriting the code, changing the way one thinks about the processing.


Of course, now I've had a comment, I can't just refactor in place, like I've done with the Python…

Thursday, October 02, 2008

1000th Post — The correct solution to a pesky interview riddle

Rather than navel gaze for my 1000th post, the answer one should really give to the tired old interview chestnut about the guys who take 1,2,5 and 10 minutes to cross a bridge, no more than two at a time, needing a torch to light their way.

The answer is -- “It's a simple shortest path traversal for a graph, where the nodes are the 30 possible states (5 entities, 4 people and a torch, with the two torch-by-itself states not allowed). So use the A* algorithm to traverse the graph. You can even deduce the links in the graph algorithmically.”

Here's the Python code:

which outputs one of the two symmetrically equivalent shortest paths

>pythonw -u ""
    2+1 -->
<-- 1
    10+5 -->
<-- 2
    2+1 -->
Time taken = 17 minutes
>Exit code: 0

Fortunately, it seems that MSFT have long given up this interview habit -- just that the rest of the world may not yet have caught up.

Doing it programmatically also allows you to investigate how stable the solution is vis-à-vis the naïve strategy of using the 1-minute guy as escort, which is only 1 minute slower than the optimum.


A little refactoring to put the times into one place (the new crossing_time function) and deduce everything from there. The use of the bits (which bits) is still a little dispersed, being there in display as well. Also changed the shortest possible distance heuristic h_score to be strictly monotonic (never overestimating the distance to the goal). Also play the path in the correct order.

Nature Notes

A magnificent, if topless, rainbow showed as I drove home this evening, the setting sun streaming into the squall that had just gone over (as in, I waited for it to pass before going out to the car).

And, driving through Bourn, a black squirrel bounding along the road-side. First one of them I've seen!

Wednesday, October 01, 2008

The Idiom Trap

While on leave last week, looking for something to read in the local Borders, I chanced upon Joel Spolsky's Best Software Writing collection of essays -- and one that struck me, in relation to some recent discussion at work about C++ (and how it made you stupid, as if C# was a silver bullet) was the one titled C++ - The Forgotten Trojan Horse, which recounts the language's success as being down to being a superset of 'C'. 

This is really also its greatest weakness.  Being treated as 'C' with a few flanges bolted on, a lot of code that is in .cpp files because it has mutated enough to no longer compile as .c, can still spiritually remain 'C', by keeping the idioms -- the comfort zone, as it were -- of the older language.  Although it was once said that "The determined Real Programmer can write FORTRAN programs in any language." -- these days, 'C' would be a more accurate target of jest than FORTRAN, leading to a style of coding that -- use of supplied APIs aside -- could be pretty much identical despite putting a C(++), Java or C# label on it.

Of course, if writing in nominal C++ and sticking to old, familiar 'C' idioms makes the code more comprehensible to a wider audience -- contrast

while ((line = reader.ReadLine()) != null)...


cont<T> sequence( (std::istream_iterator< T >( T ) ), std::istream_iterator< T >() );

for example -- there is a serious trade-off to be made when considering robustness and being idiomatic in the full language, on the one hand, and going for code that is couched in familiar terms on the other.  Not that this is just a 'C'/C++ issue -- C# evolves too, and C#1 idioms (like making two passes to remove items from a List) remain familiar, despite the IList<T>.RemoveAll() method taking a delegate being there at C#2.

It's just worse with 'C'/C++ because there is more legacy code and more legacy idiom to drag you down -- as anyone who has ever attempted to impose some const-correctness on a real piece of code, let alone trying to replace PCCHAR arguments with the appropriate choice of std::string& or std::vector<char>& using checked STL containers that will actually throw on out-of-range access, will testify.  In that case it's more than just putting the explanatory comment about the idiom (in the same way that you'd have to comment why you might be doing odd range bounds computations in the 'C'-ish code) that holds back the adoption of the idiom -- it's the pervasive nature of the change, usually way beyond that of the immediate issue being addressed, that is the impediment to positive change.

C++ has changed a lot since 1990, but has code changed to reflect that? The advice in The Pragmatic Programmer is to learn one new language each year.  Make sure that some years, the language you learn is one you think you already know, be it C++, C#, Ruby, Python, JavaScript, whatever, because they are all moving targets, and what you though was best practice can have become obsolete.

Links for 1-Oct

Emulating Java enums in .Net -- more than just symbolic constants. C#/F#

Web forms, error messages, and screen readers.

PowerShell guiding security principles.