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!

Poetic Linus

Original post date: Thu Nov 30 12:00:00 2006
Linus must have been in one of his poetic moods again ;-)


It's one of those rare 'perfect' kernels. So if it doesn't happen to compile with your config (or it does compile, but then does unspeakable acts of perversion with your pet dachshund), you can rest easy knowing that it's all your own d*mn fault, and you should just fix your evil ways.

Sunday, February 14, 2010

Slow HD due to CD polling

Original post date: Sat Nov 04 12:00:00 2006
Yesterday I upgraded our Dell Latitude to FC6, it was running FC4 and it was getting difficult to encounter some of the packages that I use on the desktop system (like Twinkle for example).
Well I can tell you that the installation went a whole lot better compared to the problems I had with the desktop machine!
There was just one thing that bothered me. Where the desktop actually seemed faster (as it was supposed to according to this, see the Performance section) the latop felt sluggish somehow. Applications were taking too long to load. When I tried measuring the drive's performance I got this:

which is way too slow for that drive. Then I remembered that I had the same problem before with this laptop and it was because of the polling KDED did of the CD rom drive (to detect media changes and such). Stopping KDED (which you can do in the KDE Control Center -> KDE Components -> Service Manager) would solve the problem.But with FC6 this didn't seem to help. I found an option called Enable CD polling in the Peripherals -> Storage Media of the Control Center, but the option was grayed out so I couldn't change it.
I finally found out today that it is HAL which does the polling nowadays, specifically the hald-addon-storage module. Killing it restored the expected performance:

So now it was time to figure out if something could be done about it. According to to this forum message it is a known problem with Dell laptops but it seems that the blacklisting it talks about doesn't work (anymore) in FC6.In the end I found out that HAL uses a set of rules to determine what to do with certain hardware which are stored in/usr/share/hal/fdi. By doing a lshal I could get a list of all the detected devices and find the information about the particular CD drive used in the laptop:

Using the information above I created my own fdi file information/30private/10-crappy-dell-cddrive.fdi with the following rules:



service haldaemon restart) everything was back to normal. Of course the system won't automatically detect any CDs being inserted into the drive, but I can live with that. (And yes, of course I filed a bug report)

Switching between Java versions

Original post date: Fri Jun 01 12:00:00 2007
It was always possible of course to install several Java versions on the same system, but switching from one to another has always been a bit of a nuisance. In Windows I started using batch files that I wrote specifically for the purpose of setting PATH and JAVA_HOME variables while for Linux Fedora you can use the alternatives command.
But I just found out that newer Java versions actually support switching versions directly from the command line:

 java -version:1.5+ ...
  
will require that the code is run with a VM of at least version 1.5 while:

 java -version:1.6* ...
  
will use any version 1.6 VM (so it won't accept 1.5 nor 1.7, but it will accept 1.6u1). Requiring a specific version of the VM is supported but is not recommended because it would tie you to that very VM not even allowing the use of VMs that have important bug or security fixes.Specifying a VM that can not be found on the system will result in an error of course:

 > java -version:1.4* -version
 Unable to locate JRE meeting specification "1.4*"

OpenJDK

Original post date: Tue May 08 12:00:00 2007
Yes, it's finally here! Welcome to the new Java era. Now let's see where this roller coaster will take us. Don't close your eyes! OpenJDK


Java Games - Bang Howdy

Original post date: Sat Jan 13 12:00:00 2007
This has already been a Java Application of the Day but I thought Bang Howdy deserves to be mentioned more. This is one of those examples that it is very much possible to write nice games using Java!



F3 and Flying Saucers

Taking a look at the latest information about that highly intriguing Java scripting language called F3 I found a reference to yet another interesting Java project I had not heard about before Flying Saucer. A strange name for a XHTML+CSS renderer but I'm not complaining, components like this are always very welcome!

Regular simplification

Original post date: Sun Nov 19 12:00:00 2006
Hey, I should do this more often! While writing the previous item I thought it would be nice to format the regular expression a bit by indenting it and adding some comment, this was the result:

# gobble any spaces
(\s*)
# main group
(?:
 # keyword - gather letters until first space or =
 ([^=\s]*)
 # option group
 (?:
  # option 1 - attribute has no quotes
  (?:
   # gobble spaces and =
   (\s*?=\s*?)
   # value - gather anything that's not a space or quote
   ([^\s"]+)
  )
  |
  # option 2 - attribute has quotes
  (?:
   # gobble spaces and =
   (\s*?=\s*?)
   # value - gather anything between two quotes
   (".*?")
  )
 )?
)
  
And while setting this up I saw that this was actually more complicated than need be so I started pruning. When I put back the resulting expression everything still worked so I must have done something right. The indented version looks like this:

# gobble any spaces
(\s*)
# keyword - gather letters until first space or =
([^=\s]*)
# optional group containing the value
(?:
 # gobble spaces and =
 (\s*?=\s*?)
 # option group
 (?:
  # value - (option 1) gather anything that's not a space or quote
  ([^\s"]+)
 |
  # value - (option 2) gather anything between two quotes
  (".*?")
 )
)?
  
And the resulting expression looks like this:

(\s*)([^=\s]*)(?:(?:(\s*?=\s*?)([^\s"]+))|(?:(\s*?=\s*?)(".*?")))?
  
A bit shorter than the first version but still way too cryptic in my book.

Regular confusion

Original post date: Sun Nov 19 12:00:00 2006
I have been working the last couple of days on adding syntax highlighting to my blog. For this I used the jEdit syntax package that I found on SourceForge. I had to change it a bit because it was a bit too focused on using it from within an editor but all in all it is not bad.
Only the XML highlighting didn't really convince me, it would just color everything blue except for the comments. So I thought I'd take a stab at parsing the attributes so keys and values would get their own colors.
But looking at the code I realized it would take some work doing it the way they did it so I decided to just at a regular expression at right point.
Thing is, regular expressions are great but when they finally result in something like this, I have to wonder "WTF am I doing???":

(\s*)(?:([^=\s]*)(?:(?:(\s*?=\s*?)([^\s"]+))|(?:(\s*?=\s*?)(".*?")))?)
  
And this is without the escaping that is necessary when putting this in a Java string! Luckily it's only a couple of slashes but it can soon get very messy.Oh, and what it does is figure out the attributes for an HTML or XML element. So if you have something like this:

<input type= checkbox name = "checkme" selected>

the regular expression will cut it up into the following tokens:
  • " "
  • "type"
  • "= "
  • "checkbox"
  • " "
  • "name"
  • " = "
  • "\"checkme\""
  • " "
  • "selected"
Of course not all of this is legal in XML, but the expression supports the most lax of the 2 formats.

Java GPL

Original post date: Mon Nov 13 12:00:00 2006
Wow, for me this definitely is an historic day, Sun will release the entire Java platform as open source! And they're not just using any license, they are actually releasing it under the GPL!
But even more incredible in my opinion is Jonathan Schwartz, director of Sun Microsystems, admitting that the final push for selecting the GPL was the Microsoft-Novell anouncement last week about protecting eachother's customers from patent violations:
By admitting that one of the strongest motivations to select the GPL was the announcement made last week by Novell and Microsoft, suggesting that free and open source software wasn't safe unless a royalty was being paid. As an executive from one of those companies said, "free has to have a price."That's nonsense.
Of course, Richard Stallman will have to revise his infamous essay "Free But Shackled - The Java Trap", but he doesn't seem tomind much, in fact he seems pretty pleased, commending Sun for taking a leadership role and saying that this makes Sun the world's biggest single contributor of open source software.
Personally I would like to see how the Linux distros will pick this up. RedHat for example has always been a big supporter of Java, spending a lot of time and effort to make sure that projects like GNU Classpath and GCJ work perfectly on their systems.
How will it all work out? Is there any use for the Classpath people to continue now that their goal of having a free implementation of the Java class libraries has been attained in one fell swoop?
And how will GCJ be integrated into all of this (because that's one project that is not going away for sure because native code compilation will always be more interesting to some people than relying on JIT compilation and the Hotspot optimizer)?
This is all so exciting! I can't wait for the future to arrive!

Syntax high-lighting test