Sunday, March 23, 2014

Multi-value maps improved a lot

Last week I sat down and decided I should pick a rather sizable project and work on it for the practice value. Midway through Java 8 was released, so I integrated that into my project. I'll get back to that in a future post.

I must say it's pretty difficult to expose yourself to all the new and cool stuff that's around when you're working as a programmer. Code bases tend to get stuck with the tech that was around when the first shovel hit the ground.

I think my biggest "huh, that's cool" about Java 8 today is how much it simplifies multi-value map semantics. In pre-8, they were pretty seriously clunky to work with.

Saturday, March 22, 2014

Conflicted about new programming language releases

I've been meaning to blog more, and post more code, so here's killing two birds with one stone :-)

I'm always deeply conflicted whenever there's a new version of a programming language I like around. On the one hand, I get to play with new and interesting features, but on the other hand, aimlessly using new features tends to lead to pretty messy code that uses them for the sake of them existing, rather than them adding something to the code.

I think a lesson I learned with C++11 is that less is definitely more with closures, but for those rare instances they are legitimately called for, they can really improve the code. It's sort of about letting applications find you, rather than going out actively looking for ways to cram lambdas into your code.

Today it happened in Java though. I legitimately produced cleaner code by using a Java 8 lambda function that I possibly could otherwise. Great success! Here is the outcome (function delegation is at line 52, in processDocument()).

Friday, November 8, 2013

Sad Hill Cemetery revisited

I recently re-watched the classic spaghetti western "The Good, The Bad, and The Ugly."

The film ends in a classic scene, where our protagonist Blondie ("The Good") is part of a Mexican stand-off at Sad Hill Cemetery versus the antagonists Tuco ("The Ugly") and Angel-Eyes ("The Bad").

In the film, Blondie has an ace up his sleeve, so he does not have to consider the rules of this problem, which is a bit unfortunate as they are pretty interesting. The trivial case where each shooter has 100% accuracy, it is clearly never a good idea to shoot first -- whomever you do not shoot will shoot you. Shooting first simply never is a good idea in this scenario. Good thing Blondie cheated, in other words.

However, when accuracy is less than perfect, things get more complex. Instead of crunching the numbers manually, I decided to let machines do my work for me, and wrote a python script to simulate the stand-off.

Assuming Blondie fires first, these are the results, which offered a few surprises
Simulation of the Sad Hill Cemetary scenario, 1000 samples/accuracy level

As expected, the high-accuracy scenario means shooting first is a guaranteed way of losing, but if the involved parties are poor marksmen, the rules change. It turns out, if the accuracy of all involved shooters is less than 50%, shooting first is in fact the optimal strategy.

But there's a third domain. If all involved are imperial stormtroopers, with accuracy approaching 0%, it won't matter who fires first; all gunslingers have approximately the same likelihood of surviving, which seems sensible, as the battle will get more drawn out who performed the opening move will play an increasingly smaller role.

The sources used for the simulation are attached below: