Monthly Archives: July 2013

Delegation

Last time, I spoke about lambdas, and left you with this comment about other features of lambdas. One that’s particularly worth bearing in mind is the ability to pass this as a lambda parameter. That means you can get access to more than just the enclosing scope variables, you can get access to the enclosing… Read More »

La Lambda

Let’s say we’ve loaded a database table into memory and now we want to do some work with it. I’m imagining a case where we’ve selected a small subset of records from an enormous table (i.e. it wasn’t cheap to fetch) and we want to pull records that meet particular conditions from our subset. Let’s say… Read More »

Cloners

Let’s say we are reading tagged blocks from within some sort of packet. The blocks being tagged means we won’t care whether they’re present, what order they come in, and can have each tag be a different length. <tag2> <tag2_data0> <tag2_data1> … <tag2_data11> <tag1> <tag1_data0> <tag1_data1> … <tag1_data8> <tag6> <tag6_data0> <tag6_data1> … <tag6_data20> <tag3> <tag3_data0>… Read More »

Ordering

Consider this: #include <iostream> using namespace std; int x = 10; int f(int& x) { x++; return x; } void g(int& x) { x *= 2; return x; } int h(int f, int g) { return f + g; } int main() { clog << "h() = " << h(f(x), g(x)) << endl; return 0;… Read More »

Don’t Take Exception

Exceptions were an enormous improvement for error handling in software. Previously we would have code like this: int a() { if( !do_something() ) return error_code_for_a; return success_for_a; } int b() { if( a() == error_code_for_a ) return error_code_for_b; return success_for_b; } int c() { if( b() == error_code_for_b ) return error_code_for_c; return success_for_c; } int… Read More »

Recursive SQL Query

Imagine these tables (I’m using PostgreSQL): CREATE Capabilities ( ID serial PRIMARY KEY, Code text NOT NULL ); CREATE CapabilityPermissions ( ID serial PRIMARY KEY, CapabilitityID integer NOT NULL REFERENCES Capabilities(ID), PermissionID integer NOT NULL REFERENCES Capabilities(ID) ); What I’m aiming for here is the ability to make a permissions hierarchy. This will let us… Read More »

Garbage Collection is Harmful

I don’t like garbage collection. Fundamentally, my problem with languages that have a garbage collected memory model is this: memory is not the only resource. Resource Acquisiation is Initialisation. RAII as it’s often abbreviated to. RAII and object oriented languages go together beautifully because the language does the heavy lifting with its automatic calling of… Read More »

Finally, No

Why doesn’t C++ have a finally block? Here’s some Java. void userFunction() { f = new Resource(); f.open(); try { // might throw buffer = f.read(); } finally { f.close(); } useBuffer(buffer); } It’s tempting, after you’ve had a finally for a while to think that it’s necessary for proper resource management in the face… Read More »