Listen on a port and do nothing

I’ve been having some problems with socket connections “stalling” in that it seems to connect fine but does nothing afterwards. I don’t really work with Python much, but it seems like this does the trick in simulating that:

https://gist.github.com/5632656

Dynamically creating tests with ScalaTest

At the Code Retreat run at Movio this weekend we watched Corey Haines do the Roman Numerals kata in Ruby. An interesting thing he did was to list all conversion in a hash and iterate over it to dynamically create the test for each conversion:

https://gist.github.com/echojc/5562397

Later while attempting to create Conway’s Game of Life using TDD I came across some tests that were repetitive in a similar manner:

https://gist.github.com/echojc/5562418

Turns out that ScalaTest also supports creating tests in the same way:

https://gist.github.com/echojc/5562425

It might take some getting used to, but I think it’s a rather nice way to run different inputs for the same test. It lets you isolate each input into its own test case without any code duplication and concisely lists all your test cases and expected outputs together.

You can see how I did Corey’s Roman Numerals kata in Scala (along with other katas I have done/will do) on my GitHub.

git completion for aliases

So I’ve been using g as an alias for git for a few weeks now (in combination with aliases for common git commands), and one thing I miss is autocompletion for things like branch names. Especially so when my branches are named something 12345678-awesome-feature.

I’ve finally found the command to do it:

__git_complete g __git_main

Alternatively, you can alias commands individually if you prefer your aliases without the space, like

__git_complete gc _git_commit

Take a look in git-completion.bash for all the mappable commands.

Colouring git diff in Gmail

Git’s default post-receive hook sends emails that aren’t monospaced and don’t have colour. Here’s a short bookmarklet to add just that. (Uses Consolas as the monospace font.)

Hippopotomonstrosesquipedaliophobia

Sometimes I think I take things a bit too far.

Birthday problem

Recently at work we came across a case where we needed to generate up to 10,000 random unique numbers. We had to fit it into 23 bits, giving us roughly 8 million different numbers to choose from.

We’ve all done combinatorics before, so we knew that if we were to randomly generate these numbers, the chance of there being a collision isn’t going to be as low as what our intuition tells us (see the birthday problem). But none of us were really that fluent with our math, so when we plugged our formula into WolframAlpha and it spit out 99.8% chance of a collision, we were sure that the problem was with our formula and not with the scenario.

I ended up testing the situation empirically in the Scala REPL and it turns out the math was right after all. Here’s the template for empirically testing the classic birthday problem:

The Dark Knight Rises

The movie was pretty good, the plot was interesting, but there were a few problems. Mainly physics, because I tend to notice that kind of stuff :)

Before I carry on, let me warn you, spoiler alert!

Read More

Potential

I think that everyone has potential. The job of any leader is to see that potential in their subordinates and to develop it into something more than just potential. That potential needs to metamorphosise into its physical manifestation.

I know my obvious potentials. I’m good with taking logical steps - solving maths problems, writing software, and even writing papers, since a good paper is a well-reasoned argument to support whatever research you’re doing. Languages can also be grouped here because languages very logical, if you consider them in terms of grammar and syntax. There are very precise rules that determine what makes sense and what doesn’t. Hence, I’m very interested in learning languges, learning about languages, and linguistics in general.

The point I’m trying to make, however, is not what potentials I have. I know what I can potentially achieve and do. The problem is that potentials are useless if they are not realised.

I can plan an excellent paper in my head. I can see it come together into a well-reasoned, neatly set out, almost artistic argument for my point. But I can’t turn that paper into reality, into a physical piece of work. I have the potential, but not the strength of will to do the work.

I don’t quit on things I say I will do. I’m going to finish my masters no matter how hard it might get. And interestingly enough, with this experience, I now understand better what a leader is supposed to do. So, as I step up to lead the charge, my underlings better be ready to follow me and transform their own dormant caterpillars of potential into a flight of butterflies of success.

How to lose a weekend of productivity

I slept late on Friday night. That would’ve been fine if it was a normal Saturday, because I wouldn’t have to get up until 11:30 (at the latest), since I have to lead choir practice at 12.

Except I had an earlier meeting at 10:30. “That’s still fine,” I thought. 3am to 10am is still 7 hours of sleep.

Except I had an even earlier meeting. At 9. So I ended up sleeping from 3:30am to 8, for a total of 4 hours and 30 minutes.

Saturday morning, I got up, made a Mi Goreng for breakfast, and grabbed a 250ml bottle of Red Bull as I left home. I would drink it throughout the 9am meeting, literally the only reason I stayed awake. But it did its job, and boosted my energy levels so that I could go through a 1.5h meeting and two 1h practices straight after without feeling tired at all. I then had lunch at Wendy’s with some friends, going home at 3 when my non-geek friends got tired of me raving on about the x86 instruction set compared to the ARM instruction and how per instruction conditional execution is really awesome.

I didn’t feel sleepy at all for the rest of the day. I was pretty surprised, given how little use Red Bull (and coffee) was during the management project week (5 days straight arrive at uni by 7am -.-). I gamed away at TF2, clocking probably more hours on Demo than I had ever before (I usually play only on Turbine and Degroot Keep as Spy or Scout, but this day I played some Payload maps with my sister).

I would’ve slept by 2am on Saturday night, but I had only recently downloaded Taiko for my iPhone and I really wanted to hit a full combo on Oni difficulty before I turned in. After 2 close failures where I missed on literally the 2nd to last note, I finally nailed it. I turned in at 2:30.

Next morning was Sunday, so naturally I had to get up early. I set my alarms (yes, plural) for 7:40am so I would have plenty of time for breakfast and to get ready for church at 9. That would mean a total of 5 hours sleep. When my alarms went off, I felt so tired I could barely walk across my room to turn them off (if they were next to my bed, I can turn them off in my sleep). I stayed in bed and didn’t get up until 8:30am. I drove at 50kph and didn’t even try to accelerate to get through a set of lights that I could’ve made it through; I was still so tired I probably would’ve crashed if I tried.

Even so, I thought to myself, “I’ve done this before - two 5h nights in a row. It’s tiring but not that bad.” Oh, how wrong I was.

I went through 2 church services, attending the first one as a congregation member and serving in the second as the pianist. Being the pianist today required more focus than usual because there’s a very particular way things have to be done in that second service of the day. Heads usually roll if things go wrong. So that sapped even more energy out of me. On the plus side, stifling yawns is now a staple in my repertoire of skills.

Then came lunch, where I racked my brains to complete Sunday’s set of words for 7 Little Words (available for both iOS/Android) before my friends could.

All in all, I probably dug too deep into my energy reserve. 1 meeting, 2 practices, 2 services, and hours of gaming, all on the back of a bottle of Red Bull and 10 hours sleep over 2 days. Red Bull lets you borrow more energy, but you have to pay that back, and pay it back quickly. I napped for at least 3 hours this afternoon, and I’m still tired enough to get ready for bed before 1am. That’s where I am right now, typing this post on my phone in my bed.

Moral of the story: using your Mana Ring eats into your HP, and if you don’t want to get sniped by a roaming enemy you better go back to your fountain as soon as possible.

Improving my Japanese

With a possible trip to Japan at the end of the year with some of my friends, it’s time to polish up what Japanese I know. Probably what sparked this was when a friend asked me to help her with some Japanese homework (and she left before I could actually be of any help >.<).

(If you’re studying Japanese, I seriously recommend using Tae Kim’s Guide to Learning Japanese. I taught myself Japanese and 80% of what I know is from that site. Another useful resource is WWWJDIC, a comprehensive online dictionary.)

I often think about random things, and on this occasion I had Let’s go to the Mall from How I Met Your Mother stuck in my head. So, I thought about the first line of the chorus:

Everybody come and play

How would you say this in Japanese? I would go for something along the lines of “みんな遊びに[くる-volitional]” (which I realise now in hindsight it doesn’t quite mean the same thing - this expression would mean “everybody come (in order) to play”). And then I hit upon the realisation that I didn’t know the casual volitional form of くる. Polite form is easy - きましょう. But what is the casual form? I settled on きよう and decided to look it up. Turns out, the casual volitional form of くる is こよう. That’s an important thing to remember.

Since I was on Tae Kim’s guide anyway, I randomly chose a section to read - the X[よう]になる/する section. Turns out, it’s an extremely nifty construction that can be used almost anywhere. To express something like

I’ve decided that I will go to Japan.

Instead of saying

日本に行くことを決めた。

You can also say

日本に行くことにした。

There’s a slight difference in connotation, I think, but I never realised that you can use する in this manner. Another similar construction presented on the same page is the form “[verb-potential]ようになる” meaning that you can now do something that you previously couldn’t, the main focus being on the “can” (i.e., potential) aspect. For example,

ピアノが弾けるようになった。

Meaning “I can (now) play the piano (when I previously couldn’t)”.

To say “improve” in Japanese, you can say よくなる. Theoretically, then, I can use this form and say

もっと勉強したから、日本語がよくなれるようになった。

“よくなれるようになった” is such a mouthful to say.