The quality of writing by Jo Nesbo (or possibly the translator?!) hits you as soon as you begin his books – this one is every bit as good as The Redbreast. Whilst some of the Harry Hole books are grisly, this one doesn’t rate too high on the bloodthirsty scale (although one incident with a vacuum cleaner isn’t for the squeamish).
Book Review: The Redeemer, Jo Nesbo
Filed under Book Review
Book Review: The Concrete Blonde, Michael Connelly
This is another book in the Harry Bosch series from Michael Connelly. There are strong connections to another book in this series, The Dollmaker, so best to read that first. I particularly liked that we followed two story lines, the first being a court case with Harry Bosch unusally appearing as the defendant, the second being a new investigation into a murder. On the personal level, we found out some more about Harry’s relationship with Sylvia – but, strangely, no mention of Harry’s daughter at all (in other books, she lives with him and is quite significant in the story). 
Filed under Book Review
F# equivalent of C++ ‘Most vexing Parse’
I first read of the C++ “Most Vexing Parse” in the Scott Meyers Effective C++ series. For reference, here’s the code:
double aDouble; int i(int(aDouble)); // This doesn't do what you think
The commented line actually declares a function i that takes a single integer parameter and returns an integer. Now, today, I saw something similar in F# that made me scratch my head and I think is similar:
let func a b c =
return a + b + c
let main () =
let sum = func a b
// use sum
0
The symptom was that my function (here, called func) wasn’t being called. Yet stepping through in the debugger, I could break on the line that called it – yet it wouldn’t step into the function! Of course, by missing one of func’s parameters in the function call, I’d actually declared sum to be a new function taking one parameter (or to use functional terminology, I’d curried a and b).
This is hard to spot when the calling code and function declaration are in separate files and when you’ve added a new parameter to the function and it still compiles but doesn’t do what you expected!
Filed under Programming
Birthday Paradox
Nice write-up of The Birthday Paradox on the BBC today.
It’s puzzling but true that in any group of 23 people there is a 50% chance that two share a birthday.
Some bright spark noticed that, with exactly 23 players in each World Cup 2014 squad, here was a topical example with which to test the theory, by which at least half of the 32 squads are expected to have a shared birthday.
Using the birthdays from Fifa’s official squad lists as of Tuesday 10 June, it turns out there are indeed 16 teams with at least one shared birthday – 50% of the total.
Filed under Musing
How to get file names under a folder in C++
As per my earlier post How to transform between a double date-time and std::string in C++, some things are surprisingly hard in C++, especially compared to writing in a .NET language. I recently tracked down this useful snippet of code on the internet in order to recursively build a list of files matching some file extension under a folder. This only compiles on Windows – if you want something cross-platform, look at boost (I wanted to avoid bringing the boost filesystem library into my project as yet another dependency).
void accumulate_files(
const std::string& folder,
const std::string& extension,
std::vector<std::string>& file_names )
{
std::ostringstream oss;
oss << folder << "\\";
std::string search_path = oss.str();
WIN32_FIND_DATA fd;
HANDLE hFind = ::FindFirstFile( search_path.c_str(), &fd );
if( hFind != INVALID_HANDLE_VALUE)
{
do
{
std::string file_name = fd.cFileName;
std::ostringstream full_file_name;
full_file_name << folder << "\\" << file_name;
if ( boost::algorithm::ends_with( file_name, extension ) )
{
file_names.push_back( file_name );
}
else if ( file_name != "." && file_name != ".." && !file_name.empty() )
{
// Recursively call into next directory
accumulate_files( full_file_name.c_str(), extension, file_names );
}
}
while( ::FindNextFile(hFind, &fd) );
::FindClose(hFind);
}
}
For production code, you’d also want to wrap the file handle in a smart pointer to ensure it gets closed properly for exception safety.
Filed under C++, C++ Code, Programming
cpplinq – functional style for C++ using lambdas
I’ve just tried out CppLinq, a C++11 library that brings LINQ-style syntax into scope for C++ programmers that are used to writing code in a functional style. I’ve been using C++11 lambdas with STL algorithms like foreach, transform, accumulate – but this syntax using where, for_each, sum and ‘>>’ to chain commands together is so much neater. In fact, it brings C++11 style very close to the succinct F# piping style that is so popular.
To use cpplinq, you can just download a single header file and include it in your code. Awesome – having just battled for hours to use some other 3rd party library which required multiple libs, source files and compiler switches, this is so easy by comparison.
This Dr Dobbs article has several code examples which act as an simple tutorial.
Filed under C++, Programming
Book Review – Riding Rockets, Mike Mullane
Having first read An Astronaut’s Guide to Life On Earth by Chris Hadfield I was interested to see how this book by another space shuttle astronaut would compare. It’s every bit as good, but in a completely different way. Whilst Chris Hadfield impressed me with his professionalism and values, someone you’d want on your team, Mike Mullane came over as more of a laugh and a guy with whom you’d enjoy a beer. But behind the terrible jokes and politically incorrect attitude, the book reveals a man with great pride, a drive to be the best and a lot more sensitivity to his colleagues and family than I first suspected. Coupled with the author’s insight into historical events concerning NASA and the shuttle program, this is a brilliant book.

Filed under Book Review
How to emulate C++11’s ‘enum class’ in C++03
One of many neat features in C++11 is the introduction of ‘enum class’ – this addresses problems arising from enum declarations such as scoping (the enum cases leak in the same scope as the enum type) and implicit conversion to integer. See Stroustrup’s C++11 FAQ for more details.
However, in the papers that motivated the new language feature (N1513 and N2347) they discuss the current workarounds for C++03. One such workaround is to define a class to represent the enum type – it’s much more verbose than the new C++11 feature, but it solves the scoping and conversion issues.
// Header file
class Weekday
{
private:
// Note that the private enum cases have underscores to differentiate
// from the public cases
typedef enum Weekday_ { Mon_, Tues_, Wed_, Thurs_, Fri_, Sat_, Sun_ };
Weekday_ value_;
public:
static const Weekday Mon, Tues, Wed, Thurs, Fri, Sat, Sun;
explicit Weekday( const Weekday_& value ) : value_( value ){}
bool operator<( const Weekday& rhs ) const { return this->value_ < rhs.value_; }
bool operator==( const Weekday& rhs ) const { return this->value_ == rhs.value_; }
bool operator!=( const Weekday& rhs ) const { return !(this->operator==(rhs)); }
};
// Source file
// Definitions for the public Weekday instances
// in terms of the private enum
const Weekday Weekday::Mon( Mon_ );
const Weekday Weekday::Tues( Tues_ );
const Weekday Weekday::Wed( Wed_ );
const Weekday Weekday::Thurs( Thurs_ );
const Weekday Weekday::Fri( Fri_ );
const Weekday Weekday::Sat( Sat_ );
const Weekday Weekday::Sun( Sun_ );
That’s it – I’ve been using this recently and it works pretty well. I also added a “to_int()” member and a static “from_int()” member to explicitly convert bewteen the enum class and integers – the implementation is simply a switch over the enum cases.
Filed under C++, C++ Code, Programming
Restaurant Review: Clos Maggiore, Covent Garden, London

I had a lovely birthday dinner at Clos Maggiore, near Covent Garden in London. This restaurant is one where you have to book long in advance (in my experience, at least a couple of months) – but it was worth the wait. For a romantic occasion, it would be hard to beat the conservatory, with its blossom trees and roaring wood fire. The menu was excellent, and the service impeccable (if slightly arrogant – when I commented to our waiter “It’s a very good menu”, he replied “No, it’s an excellent menu”). Every dish was superb – I can vouch that the braised shoulder of rabbit (starter), the guinea fowl (main) and chocolate fondant were all exceptional. As an extra touch, for my birthday, they piped a suitable greeting on the dessert plate in chocolate (and again for the petit fours with coffee).

Filed under Restaurant Review
Book Review: The Panther, Nelson DeMille
This John Corey thriller from Nelson DeMille has all the usual ingredients to make a great read – thrust Corey into a highly pressured world where gun play is the norm; include his wife, the lovely Kate Mayfield, to offset the crass and politically incorrect commentary from Corey; throw in an arch-villain as the nemesis for this adventure; finesse with conspiracy theories regarding Corey’s historical emnity with the CIA. The scene setting in Yemen was vivid and had the hallmarks of DeMille’s attention to detail, and yet, this book over-stayed its welcome by a couple of hundred pages. 
Filed under Book Review