Friday, 10 August 2012

Perl, Floats, and Memcached

Oh Perl, I love you, but sometimes you do drive me nuts.

The Problem:  Consider the following code:
use strict;
use JSON::XS;
use Cache::Memcached;
use Data::Dumper;
my $memd = new Cache::Memcached { 'servers' => [ "127.0.0.1:11211"], };
my $x={ "f" => [[134,1.7],[134,2]] };
print "before: \n";
print encode_json($x);
print "\n";
$memd->set("key",$x);
print "after: \n";
print encode_json($x);
print "\n";
You get this as the output:
before: 
{"f":[[134,1.7],[134,2]]}
after: 
{"f":[[134,"1.7"],[134,2]]}
Now, why is 1.7 now a string after the set() call? It's the same object I am encoding to json. So the serialization process in memcached's set() has stringified the float 1.7 into "1.7".

You can actually replicate the same behaviour without memcached entirely, if you just do a Dumper($x) in between, the after Dumper() $x will have 1.7 as a string instead of a float.

I know Perl does not have strict typing, and that you can add 0 to a string (or multiply by 1) to make a variable into numeric representation. However, the above is definitely a problem.

The Solution, is to use Storable module and freeze the object $x before the store, and then of course, thaw $x when you retrieve it back out. See below:
use strict;
use Storable qw(freeze thaw);
use Cache::Memcached;
use JSON::XS;
my $memd = new Cache::Memcached { 'servers' => [ "127.0.0.1:11211"], };
my $x={ "foo" => [[134,1.7],[134,2]] };
print "before: \n";
print encode_json($x);
print "\n";
$memd->set("key", freeze($x));
print "after: \n";
print encode_json($x);
print "\n";
my $out=$memd->get("key");
print "thawed: \n";
print encode_json(thaw($out));
print "\n";
print "memcached version: ",$Cache::Memcached::VERSION,"\n";
print "json xs version: ",$JSON::XS::VERSION,"\n";
print "storable version: ",$Storable::VERSION,"\n";
You get this as the output:
before: 
{"foo":[[134,1.7],[134,2]]}
after: 
{"foo":[[134,1.7],[134,2]]}
thawed: 
{"foo":[[134,1.7],[134,2]]}
memcached version: 1.30
json xs version: 2.26
storable version: 2.25
Voila! Everything stays as it is!

Tuesday, 19 June 2012

340 shots to Olympic gold

A friend recently asked me, how do you get to the Olympics (in shooting)? I hope this post will answer that, if in an not really helpful manner.

To get on an Olympic team, you have to on the National team. Team trials is the nationals. So, you go to compete at the nationals. You will shoot the course of fire twice, that's 60x2 or 120 shots. Then you shoot finals, no problem, you just shot 120 tens in a row, so 10 more in finals is easy. After 130 shots, you've earned a spot on the National team.

Next is to earn a quota spot for your country, and you can do that at World Cups (there are 4 each year), or at continental championships like the CAT Games (championships of the Americas tournament) and the Pan Am Games. The last two are only held once every 4 years (like the Olympics). So, you go and compete at a World Cup, shoot your 60 shot match with all 10s, and enter finals with a world record. Once there you shoot another 10 shots, and win the world cup, and thus securing a quota spot for the Olympic games. So far so good, you've fired 200 shots so far. But wait, the quota belongs to the country. You have to compete against your fellow national team mates for it.

No problem. They probably are a little terrified. So you enter a quota selection match. Shoot 60 shots plus 10 shot finals, and voila, the quota spot is yours to keep. At 270 shots fired, you've earned your Olympic ticket.

Now you are at the big show. Lights and cameras everywhere. You do some press interviews, sign some autographs, and enter the Olympic range for your match. As with before, 60 shot match, all tens, and 10 shot finals, nothing but 10s.

Congratulations! You have done it! In 340 shots, you've gone from world unknown to Olympic gold medalist! Not to mention setting numerous records in the process. Give yourself a hand and go celebrate with some ice cream!

The above is assuming you are shooting an Olympic event with 60 shots per match, something like men's air pistol, or 50m pistol, etc. Certain other events may have more or less shots required. Women's 10m air pistol can be won in as few as 240 shots. Women's 25m pistols requires a few more, 380 shots, since finals in sport pistol are 20 shots.

Thursday, 19 April 2012

The madness of our daily lives

I was walking to work after getting off my regular bus this morning, and I was thinking about how crazy the whole thing was.

I have 24 hours per day.

I sleep about 7 hours per day. 17 hours left.

I work about 8 hours per day. 9 hours left.

From door to door, I spend 3 hours per day in commute from/to work. 6 hours left.

6 hours. That's how much time I have in total, per day, to myself.

...

Wait, I spend 3 hours per day commuting to/from work? Actually, that number can be even bigger in winter months.

Even at 3h/day, that means I spend, in any work week, almost 2 whole work days on the road.

That means, in a year, I spend nearly 100 work days, on the bus, on the train, or in some other vehicle going to work, or coming home from work.

Isn't that insane??

When I lived downtown, my commute time was 1 hour max, and that is if I walked the entire way. However, I was unhappy because of the lack of space. Now, I live in a big house in the boonies. The living environment is certainly much better, and I enjoy my time at home, but the commute time is significantly increased.

I can't imagine I am the only person in this situation, especially considering the real estate prices downtown.

So what's the solution? Telecommute? Move?