def qsort(list:List[Int]):List[Int] =
{
 list match {
 case x::xs => qsort(xs filter(_<x) ) ::: x :: qsort(xs filter(_>x))
 case Nil => Nil
 }
}

print(qsort(List(4,6,5,0,2,3,1,7,8,9)))

Short and sweet!…


Hacked!

26Sep09

OK, I was unfair on Google. The site had been hacked. I (or my ISP) should have been more careful. Yet, I don’t see what I could have done better, short of hiring a bunch of hackers to verify my ISP’s security procedures. Cause, really, there’s nothing in that web site as such that would allow it to be hacked through code injection or something. Or at least I don’t think there is, but I’m not a hacker. I am very apprehensive now, and totally daunted by the massive consequences. I can apply for a review of the site, but I can’t imagine a bunch of people on the other site dealing with these very efficiently. And how would they be in a position to verify a site’s security, unless they are better hackers than the best out there. So, once you’re hit, you are, through no fault of your own, destroyed. You may question that bit about the no fault, but as much as I can educate myself on hacking techniques, I’ll never be as good as the kids out there, I have a day job to do too. Beyond maintaining good passwords and choosing your ISP carefully, I don’t really see what Joe-the-webmaster can do to stop it. So, anyway, once you’re hit, you’re practically destroyed. That site will probably never show up at the top, the way it used to. Even if they wanted to not be evil and investigate and judge what’s right or wrong, there’s no way they’ll have the capacity, there’s probably millions like me out there. Not to mention the moral responsibility of being the judge of something that will ultimately result in actual wealth redistribution. Grumble, grumble, grumble. Simple life in the woods seems like a tempting proposition.


I’ve been asking this question for some time now. When will Google become the next corporate monstrosity that we all love to hate? Two events from the past 24 hours tell me that time is coming.

First event. I got an email from “adwords-support”, saying that “Your account XXX-XXX-XXXX has been suspended because we’ve determined that there’s a high probability your site may be hosting or distributing malicious software.” The site sells window blinds! Honestly?! Malicious software? All you can get from that site is plain, standards compliant HTML and CSS! What a bunch of plonkers! Oh, sorry, it’s not humans that do that, is it? Or should this be, what a bunch of plonkers wrote that rubbish? Then I go to the site, and I see that it’s been blacklisted. It’s a reported “attack site”. Wait to be attacked by the avalanche of designs of blinds and awnings we have, it will blow your mind and make you fall in love with a penguin.

Anyway, that’s not the problem. Everyone gets it wrong. But this is a paid service. I have come to expect from the real world, that when I pay for service, any glitches in it would not affect my friend’s company in this MASSIVELY detrimental way. MASSIVELY detrimental. No warning! Nothing! Bang – game over! You have been shot.

The other problem is, what do I do now?? There’s a link in the e-mail, saying, if you believe this is wrong, contact us. I won’t be contacting a human though, will I? They don’t do “Customer service” there. That’s for the old fashioned, stupid companies that like to take an evening off once in a while. Plus, the damage has been done. I hope it ends up in the papers (Bulgaria is a small country).

Second event. I was busy at work, so I had forgotten about that. At lunch, I thought I’d have a look at my email on my internet enabled phone. Gmail, to be precise. I use the phone’s browser to read it, because I’m not that hot on the email client. So, only several hours after the adwords rubbish, I get a “We’re sorry, but Google Mail is temporarily unavailable. We’re currently working to fix the problem. Please try logging into your account in a few minutes.” Well, you know what, Google, I have come to rely on your Gmail thingy. Not just for email. For remembering things. It’s my external RAM. Because your search is so good. But is it, now?… It certainly was. But so was its reliability. I hear it blew up recently too, though I didn’t get caught in that. Now, I’m thinking, hang on…. I will be a very, very, very sad boy if my email data goes down the drain. Like, properly sad. Like The Little Prince for his rose, that caliber of sadness….. VERY sad! Can I trust this thingy out there somewhere in the cloud???


I tried to illustrate how you convert between the two. This is what came out. Probably not entirely correct, but it works. It can be mixed and matched for legacy apps.

jcl-and-slf4j


Ever since Microsoft decided to remove the file search that actually worked in Windows XP, I’ve been trying to find an easy to use search tool. Turns out it exists – the findstr command line tool. so, for example,

findstr /L /S base.url *.xml

finds all xml files containing the string base.url. It also works for binary files, so when you need to see if a jar in a big list contains a class, you can do

findstr /L SAXSourceLocator *.jar

and it lists the files (together with some junk, but it’s useful none the less. the /L switch means literal comarison, but it can also do regex.


So, I’m writing this constructor here:

public Square(double x, double y, double size, double gap) {

  …

}

Then I think, really, these should be BigDecimals, because I’m modeling a rectangular area on a grid, which could be of any precision and being maths stuff has to be precise, so I change it to:

public Square(BigDecimal x, BigDecimal y, BigDecimal size, BigDecimal gap) {

  …

}

Then I write a test, and I go:

Square square = new Square(new BigDecimal(“1″), new BigDecimal(“1″), new BigDecimal(“0.1″), new BigDecimal(“0.003″));

Note, these could be doubles instead of Strings, but then we’re back to square one – if you want your BigDecimal to be precise it has to be made with a String literal, not double – once you’ve done a double literal, you’ve lost it. Then I think, wait, I’ll have to do this again in a few minutes, that will be an awful lot of “new BigDecimal” there, so I go and add another constructor:

public Square(String x, String y, String size, String gap) {

  …

}

 Right, now I can use it like this:

Square square = new Square(“1″, “1″, “0.1″, “0.003″);

Great! Except it kind of isn’t… Why should I write numbers as Strings?! What happened to the type safety and all that? I want my Scala toy back :(


The new (well, not that new now) Windows search is way annoying, so I thought may be I could use Powershell for searching. This is how to do it (best do it on one line):

 foreach ($file in get-Childitem . -include *svn* -force -recurse)
   { $file.fullname }

The -force is to make it include the hidden files in the results. A bit shorter, but arguably less readable if you don’t know Powershell:

 get-Childitem . -include *svn* -force -recurse
    | foreach ($_) { $_.fullname }

I bet you can go even shorter, I don’t really know Powershell, though it loos very, very powerful. Naturally.

Anyway, now I can do what I originally intended and blast all the svn info from my project, like so:

 get-Childitem . -include *svn* -force -recurse
   | foreach ($_) { remove-item $_ -recurse -force }

I had this funny little Scala actor related bug today. Imagine you want to process a few things in parallel. So you go:

val processor = self
jobs.foreach { job =>
  actor {
    processor ! (job.id, job.run)
  }
}
// Merge results
for (i <- 1 to jobs.size) {
  self.receiveWithin(1000) {
    case (jobId:Int, result:JobResult) => mergeResult(result)
  }
}

All good. Then you realise that one or more of the jobs Continue reading ‘Make sure you get all your messages in your Scala code’


How often have you done something like this? -

public Result calculate() {
  long start = System.currentTimeMillis();
  Result myResult = doSomethingLongAndComplex();
  log.debug("Crunching took " + (System.currentTimeMillis() - start) + "ms.");
  return myResult;
}

It’s kind of ugly when you do it a few times and it does clutter the code. The need for defining an explicit myResult var is even more annoying. So, today, I added Continue reading ‘Scala makes things expressive – timing’


Logging in Java can be messy. Especially the dreaded isDebugEnabled. Look – a class that calculates something:

import org.slf4j.LoggerFactory;
class SomeCalculator implements Calculator {
  private final log = LoggerFactory.getLogger(getClass());
  public Result caclulate(Input input) {
    if (log.isDebugEnabled()) {
      log.debug("Input is " + input);
    }
    Result result = input.times(64).shift(LEFT);
    if (log.isDebugEnabled()) {
      log.debug("Result is " + result);
    }
    return result;
  }
}

We have a class which has one line that actually does something and loads of lines that Continue reading ‘Scala makes things tidy – logging’