Recursive string repeat

Following on from Axel's mind blowing string repeat function, I decided to see if I could use recursion to implement the same functionality.

Turned out to be quite easy. I did it with TDD, while remaining faithful to Axel's implementation. I've pushed the code to github under connrs/recursive-string-repeat but I will replicate the function here:

function repeat(num, str) {
    num = Number(num);

    if (num === 0) {
        return '';

    return repeat(num >>> 1, str + str) + (num & 1 ? str : '');

I know that JS perf testing isn't really important for something like this, but I wanted to do it as part of the experiment. Here are the test results compared to Axel's version. Looks like for loops win!

I must admit that it was quite fun to try this out.

Published 30/01/2014 15:35

Last moment test

I just wanted to be convinced that Google+ was truly indexing me at this speed. It would be nice if they'd actually display this activity in my timeline!

Published 16/11/2013 23:19

Node module: email-stream

As previously mentioned, new features in my blog are slowly being rebuilt with #tdd and published to #NPM. the latest of which is email-stream.

The blog publishes information to an #ActiveMQ server which a secondary application listens for. This notifications app needs to build nice emails from the notifications that it receives. I took a look at some of the existing solutions. None of which really did streaming. I really didn't want a load of control flow logic in my notification class so I set about reading the RFCs that made up the email spec.

As I knocked out the first batch of tests, I quickly realised that there was more to refactor than first anticipated. As I refactored out each stream, I saw another stream hiding within it. By the time I'd managed to refactor and publish all the submodules, the actual email-stream module practically needed to be rewritten.

The rewrite was a total pleasure because the component modules made it so simple to build bigger parts.

Everything is streaming. Want to add text, HTML and an attachment? Streams.

The simplicity of the streams2 API means that you don't have to use them yourself. Just create a new PassThrough stream and call the .end method with your content as its only parameter:

var PassThrough = require('stream').PassThrough;
var text = new PassThrough;
var email = email stream({
  // other options
  text: text

text.end('email text');

Quite a number of my tests went like this. Anyway, that's #emailstream.

Published 08/09/2013 16:15

Holiday almost up!

Well, besides a little bit of a shared tummy bug, resulting in some late night mopping up, I'm ready for home. Looking forward to a nice relaxing weekend of code...

Published 16/08/2013 12:03

Holiday: computerless (All I need is Vim)

So I'm taking a week off to go and see the in-laws and also to catch my breath from my current major project.

I don't really want to talk about the work project (needless to say that its another prestigious Heathrow Airport project) but the fact that I'm panicking about having no laptop. #node is my latest passion out of hours; I'm writing my blog and extracting open source modules in order to publish them on #npm. I've had a few ideas over the weekend and I don't think I'll be able to code anything until the next weekend!

While most of the work going in to my blog is complete, I'm building a background services platform to automate notifications.

In the past, I would have built big PHP apps in #cakephp that did everything. New comment on a blog post? Better put a mailer in to the app to email me with updates. Trackback ping? Better build a class to fetch the source URL to verify it.

Instead of doing this in #node, I'm running #activemq on the box and building an app to listen in. This app will do the mailing, parse the pages. I might even split those apps in to their own distinct apps. I'm quite pleased with this as it means that my models and classes are geared towards specific purposes. The Post model in the blog app is geared towards fetching posts and saving new content. But the post model in the notifications app can deal exclusively with passing information about who to notify.

As soon as I get in front of a laptop I'll get cracking. I could really do with vim running on this phone for testing some of these ideas.

Published 12/08/2013 11:17

Published on NPM: find-hashtags

I've just released another package to NPM that is used on this site to extract hashtags from blog content.

Published 22/07/2013 20:57

WebMention client module for node.js

I just published webmention-client (GitHub repo) to #NPM. I extracted another module, lookup-webmention-server, as it seemed sensible to extract that little module out again. I have an issue raised as the lookup module doesn't search the document body in addition to the link header but I'm otherwise quite happy with the progress here.

It was nice to be the first to create an implementation for something. Even if that something goes unnoticed and unused!

I will probably extract ping-webmention-server as well but bug first, extraction second.

In addition, I'd like to see if I can remove the request dependency. I love request but I feel that it's overkill to use such a big module just to ensure that 3xx redirects are followed and that the correct http/https module is used. I don't know. Thinking about it, I do like request a lot. And it's tested.

Published 09/07/2013 23:09

So long Google Reader

I just wanted to say goodbye to Google Reader that has served me so well, for so long.

Published 01/07/2013 14:41

Writing my own software again

So I have finally started to write my own software again. It has been absolutely years since I last had my site running on some sort of hand rolled blog software. This time I opted to write a node.js application from (almost) scratch. I wanted to gain some real experience structuring an app without relying upon some pre-built systems.

It was really nice to take some time to think about how I used to hand roll my own blog and how I could make this new iteration so much more. There's no 'admin' right now as I'd really like to focus on frontend features than worry too much about how to manage it. So I'm currently writing this in a SQL client app rather than some sort of textarea on an admin URL. Fun!

Hopefully, this could see me writing more!

Published 15/06/2013 07:58

A month without caffeine

Introduction: The Problem

For a few months I’ve been getting headaches at weekends; they’ll usually manifest in the morning but occasionally continue throughout the day. I thought I was just getting old but after a while I realised that it was because I’d started taking a drug at work that I didn’t take at home over the weekend. I was getting caffeine withdrawal at weekends because I didn’t have the habitual routine at home that I had 'in the office.'

The Challenge

After deciding that I’d had enough, I went cold turkey. And let me tell you that it was hardcore. At first, I did some in-depth research (Google) on the subject of detoxing and found it all pretty scary. Terms like mind fog, drowsiness, brain blur, severe headaches, severe fatigue and the rest gave me pause for thought: Should I bother with this or should I just maintain this addiction for the rest of my life?

No way; I wanted out. I hate the idea of a dependency on something to make me who I am. I don’t mind the idea of having a perk like that but the dependency is a turnoff.

A couple of hours later I let my wife know and then strapped myself down (not really) and just stopped with tea, coffee, chocolate, cola, Sprite, 7up, Lucozade and quite a lot more. It’s absolutely insane how much stuff they stick caffeine into; I actually struggled to find a non-alcoholic non-caffeine drink that I actually felt comfortable drinking when I went to the pub a few days later.

Day 1—Oh this isn’t too bad

The next morning I woke up preparing for a horrendous headache and was thoroughly overjoyed when I felt fine. 3 weeks of migraines my shoe! I must confess to being a little tired and grumpy in the morning but I kept my nerves in check and got through it.

Day 2—Oh my brain

I woke up with the worst headache ever and that wasn’t the worst of it. The headache paled into insignificance alongside what I’ll forever think of as the brain fog. The brain fog is a range of symptoms like an inability to focus on a conversation; an overwhelming magnetic attraction applied to your eyes from the ground when attempting to make eye contact; memory loss and short term memory trimmed down to minutes; and a general feeling that your ears no longer understand your native language.

This was my lowest day; I nearly went to the kitchen to make the strongest brew ever. I resisted, went to buy some ibuprofen and got through the day

Day 3—Road to recovery

Day 3 was surprising. I still had a headache but the fog already began to dissipate. Partially thanks to some pain relief, I was able to actually do some work. Result.

Day 4 to 28—Light at the end of the tunnel

Throughout the next 24 days I talked a lot about how I was going to enjoy a nice brew at the end of it all. As the days progressed I lost the desire to have that trophy cuppa. In fact, at day 28 (last Saturday) I didn’t bother. I wish that I could say more about caffeine cold turkey but the mind fog was the easiest thing I ever beat.

After all that trouble

I don’t think I’ll be bothering with tea at all. I had a cup of tea yesterday and it didn’t blow my mind and I didn’t descend into craving caffeine either. I like the idea of it being a weekly treat be it a bar of chocolate or a cappuccino at a café.

Give it a try at some point. I think that the detox itself is quite revealing and, while I don’t think it’s something that everyone need endure, if you’re one of the small number of people who just did it for the routine then I you’ll benefit; try replacing it with something healthy like a fresh fruit juice.

Next week in This Week in Detox…

Alcohol? Been there done that you weaklings. Fatty food? Internet? Hah. Cordials? Who knows ;)

Published 07/07/2010 11:00