Tuesday, March 9, 2010

Curried and Confused

In the article Curried and Confused on coderspiel the author gives us this example of Scala code that people supposedly think is too difficult for regular folks:

def sum(nums: List[Int]) = (0 /: nums) { _ + _ }

He then goes on to say that it might be because of the /: symbol, but that this version does not make it any better:

def sum(nums: List[Int]) = (nums foldLeft 0) { _ + _ }

And this is the point where we differ completely. For me if the first version hadn't been called "sum" I would have had no idea what the code does. The moment I read the second version and I saw "foldLeft" I knew this was about an application of some function to a list of numbers resulting in a single result (hence the folding).

For me this isn't about reading (nums foldLeft 0) and not knowing it's the same as nums.foldLeft(0), you learn that once when reading about Scala's syntax and it will stick. Maybe you need to think about it a bit more the first couple of times, but it's not hard, the basics are still the same, the order is still the same, you can still think: "I've got this object nums on which I'm going to perform a foldLeft and... what's this 0 here? Oh must be an argument".

It also isn't about the whole closure bit where you suddenly see braces at the end of a statement, because getting used to Functional Programming you know that this is normally part of this brand new toolbox you've got. The _ + _ part does pose a bit of a problem for Scala first-timers, I agree.

But in the end for me the real problem is this /: symbol. What does it mean? If I see it, it doesn't ring any bells! There is no mathematical symbol that looks like this, in fact it looks like the symbol we've been using for ages now to denote division!

This is about readability, I always agreed with the Java-tenet where they would say that you tend to read code many many more times than that you write it. And it might be read by many people who don't have a way to look into your mind so it's important to write code in a way that it's function is as obvious as possible.

/: is NOT obvious.

But reading foldLeft is, once you've read at least once about folding and what it is used for. Because it doesn't matter in the end which language you use to implement the folding, it's a concept that's easy to grasp, but what happens if you have to go and learn by heart all the strange symbols that one can come up with? Especially when each language decides to use different symbols to express the same thing.

I now understand why they left out operator overloading in Java.
(yes, I know that in Scala this is not operator overloading strictly speaking, take your nitpicking somewhere else)

Monday, March 8, 2010

Regular expressions and backreferences

While trying to find a way to match quoted strings in a text taking into account that you can use both double and single quotes I first started looking at using two separate sub-expressions OR-ed together, but then I started wondering if it was possible to say something like "match a sequence that's exactly the same as that other sequence that you matched earlier". After some looking around I found the answer in backreferences. The following will match a single or double quoted string for example:

(["']).*?\1

Sunday, March 7, 2010

Heavy Rain

I started playing Heavy Rain today and I must say that I really like what I've seen so far! The quality of the graphics and the 3D models isn't too great, it's easy to see the typical "broken" lines where curves have been divided up into separate polygons for example. People's faces tend to have this glassy-eyed look and depending on the person can have this "plastic" look to them. Movement around the world can sometimes be a bit of a chore for somebody like me who's not used to a console's controller but to mouse and keyboard.


Now those were the main bad points. The good points are: it's just a fun, engrossing game with a great atmosphere. I'm not even sure if you could even call it a game as such, although it definitely has game aspects, like when you are an FBI agent looking for clues with your futuristic scanner glasses. Then there are the "action" sequences which are like those horrible console, press the right button at the right time, sequences... except that this time they aren't horrible. There doesn't seem to be a way to do anything really wrong, the game doesn't punish you for reacting 5ms too late, the sequence just takes a different turn.


As you can see in the video above you just walk around until some symbol appears indicating there's something to do there. There are lot's of little things to discover, most of them are just there for fun, to add some depth to the entire thing, but luckily most of those disappear after the first time you use/perform them so you don't get swamped all the time with symbols floating in the air. It also keeps you focused on the story, which turns this more into an interactive movie than just a graphically advanced adventure game. Which might turn some people off because (so far) it isn't very difficult, I don't think you will ever feel much of an achievement having encountered the solution to a particularly devious puzzle for example. But I still think it's worth it because the story is just so compelling. For a movie it might be your average psychological thriller about trying to catch a serial killer, but for a game it's great stuff that hasn't been done enough before.

Wednesday, February 17, 2010

Scala "puzzler"

November 2008 I encountered Scala and started looking into it. Thinking that some of my friends, who are also in the IT business, would be interested in hearing about my experiences I started writing them a series of mails with some of the things I learned. Mu idea is to publish them on this blog, translating them to English at the same time so it can reach a broader public.


Yesterday I bought the book "Programming in Scala" and I'm reading it at my leisure. And only a moment ago I encountered a warning that made me think "argh, why did they do that?".

It turns out there are 2 ways to define a function/method in Scala, one goes like this:

def multiply(x : Int, y : Int) = { x * y }

and the other one like this:

def multiply(x : Int, y : Int) { x * y }

the first one has 2 integer arguments and because of Scala's "type inference" it will also return an integer. This is something you don't have to specify yourself (you could do so and it would look like this def multiply(x : Int, y : Int) : Int but that's not important right now). The 2nd form also has 2 integer arguments, but because we left out the =-sign the result of this method is always "Unit" (like "void" in Java). In other words, the result of the expression will be thrown away quietly.

I think this is one of those things that can really bug you for hours without being able to figure out what is going wrong exactly. It seems like an unnecessary "shortcut", I would have preferred to explicitly define the result as being of type Unit in the case I really wanted to throw away the result.

Monday, February 15, 2010

Notion Ink´s Adam tablet

I've just watched this video and I must say: color me impressed! When you read the comparison with Apple's iPad you'll see that Apple will have a difficult time convincing people that their product is really worth the price they're asking for it. I mean, this Adam thing actually has all that you would expect in a product like this: regular USB ports, HDMI output and even a swivel around camera. Overall a really nice job, let's see if they can pull it off. They're supposed to come out in june so Apple willl have a 2 month head start.

Linux sound problems

Original post date: Fri Sep 07 12:00:00 2007
Ok, just learned that having two sound cards in your system can lead to all kinds of trouble.
Some time ago I had bought some headphones from Altec Lansing that came with an USB adapter. Not expecting it to work in Linux I plugged it in, but lo and behold it actually worked and I had now two sound cards in my system according to Fedora's Soundcard Detection utility. So far so good.
Soon after I removed the USB module and just connected the headphones directly like I had always done and life was perfect.
Until quite some time later I had to reboot my machine and suddenly I was getting all these weird ALSA errors from all kinds of programs (see for example the image of the pop-up that appeared at KDE startup).


The strange thing was that the Soundcard Detection program reported everything okay, the right sound card was detected, I could hear the sample and on top of that several programs did actually produce sound. So what was going on here?
Finally today I got a hint trying to run alsamixer when it gave me the following error:
alsamixer: function snd_ctl_open failed for default: No such device
Now this was something I could google for. So I ended up with several leads of which two led to the final solution:
  • ALSA user list which didn't really have a direct solution for my problem, but it made me look at the modules where I foundsnd-usb-audio. So what was that doing there when I had removed that USB adapter ages ago? Of course/etc/modprobe.conf was the "culprit" here. Somehow plugging in the USB adapter had inserted some lines into the file but removing the adapter hadn't deleted them!
  • LinuxQuestions.org pointed me to some files I had never heard off: /etc/asound.state and /etc/asound.conf. The first didn't seem too interesting, but the second had things like card numbers which made me think that maybe they had gotten mixed up somehow because for a short time I had two sound cards in my system. So I removed both and restarted the Soundcard Detection program. And like I had imagined the numbers were different in the new asound.conf!
So everything seems to work again. Now to figure out why the microphone only picks up sounds very faintly.

Linus and Windows

Original post date: Mon May 07 12:00:00 2007
Well, at least we know now how Linus feels about Windows:

I'm not claiming that the current code cannot work. It clearly *does* work for a lot of people. But I'm claiming that it's STUPID. So don't argue that "it works". Windows works, kind of. That doesn't make it less stupid and badly designed!