Tech

My First Scala Program

Posted in Tech on August 3rd, 2009 by admin – 1 Comment

A while back for my birthday, I got the Odersky Programming in Scala book and today I finally got a chance to crack it and play around. I had been interested in trying to pick up one of the new wave of dynamic JVM langagues like Groovy, JRuby, or Jython and had settled on Scala. I had read a suggestion that Project Euler would make for a fertile playground of test problems to learn on, and having already done the first several in Java that rang true for me. So, here is my overly complicated solution to the simple first problem..

//If we list all the natural numbers below 10 that are multiples of 3

//or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

//Find the sum of all the multiples of 3 or 5 below 1000.

import scala.actors._

object Adder extends Actor {

var sum = 0

def act() = {

react {

case (num: Int) =>

sum += num

act()

case “REPORT” =>

Console.println(“Total = “ + sum);

}

}

}

object ProblemOneRunner {

def main(args: Array[String]): Unit = {

Adder.start

for(i <- 1 to 999) {

if(i % 3 == 0 || i % 5 == 0 ) {

Adder ! i

}

}

Adder ! “REPORT”

}

}

It starts out looking something like a Java program with an import statement, although Scala uses the _ notation to import everything instead of * since * is an identifier. Scala supports a lot of flexibility in its import statements, they can show up anywhere and you can use them to remap names or set up exclusion filters – but lets not get to far astray.

Next, you’ll notice that my program has two objects and no classes. Its not that Scala doesn’t have classes, its just that Scala classes don’t have the Java concept of static – if you want a singleton object, you use the object keyword. The solution I have above uses one singleton as the main program entry point and another Adder singleton that extends Actor. This is just so I could play with Scala’s built in multithreading and message passing support – obviously this problem doesn’t really require it but I thought I’d try it out anyway for fun.

The Adder has its own sum variable it uses to keep track of the numbers it will add up – Scala uses the var keyword for variables that can be reset and val for ones that can’t. You’ll notice the lack of any type declaration here, its not that Scala variables are not typed, its just that it uses type inference to figure out what they are. Also, Java users will notice the lack of semicolons – semicolons are optional in Scala in most cases. The Adder overrides the act method, the entry point for threads, and then calls react which will look for a message in the actor’s inbox and if it doesn’t find one, put the actor to sleep. Unlike some of the other constructs Scala provides for Actors like receive, the thread will die at the end of react instead of looping so we have to put a call to act() at the end of the int message we send it to keep it alive.

In the main method, we start up our Actor thread, then loop through 1 through 999 looking for numbers divisible by 3 or 5 to send to our Adder, then once we are done we send a message to the thread to report and finish its loop. Again, Java users will notice the easy condensed for loop and the ! message passing syntax.

I’ve barely scratched the surface of Scala here, I’ll try to get a couple more problems done and posted that likewise abuse language features as I get time.

What a Jaunty Macbook you have there…

Posted in Tech on April 28th, 2009 by admin – Be the first to comment

I’ve been taking another stab at dualbooting my Macbook1,1 with the release of Ubuntu 9.04. As a mostly-happy Macophile, why dualboot you might ask? A couple of things – a weakness for playing around with Unix operating systems (a bad habit I picked up in college) and freakin Java 1.6. The problem is Apple decided not to release a 32bit version for 1.6 and my Macbook is the older Core Duo version and thus not supported. Now, normal people might sigh and stick to 1.5 or take this as an opportunity to buy a new laptop. The slightly more practical (or cheap) who refuse to give up probably turn to soylatte, the port of BSD to OS X (Metal L&F only – no pretty Mac L&F). I, however, with more disk space than sense, turned to rEFIt to set up a dual OS X/Linux setup.

I won’t detail how to setup a dualboot system, there are enough tutorials out there if one searches for them. When I first setup my laptop I installed ubuntu  8.10 and quickly ran into  two annoyances  – an error message about my keyboard layout and the fact that by default the machine is very hard to use since the touchpad causes the cursor to jump around randomly while typing. Not in a mood to research them at the time, I got distracted and sadly it wasn’t until 9.04 that I tried again. Both these problems persist with Jaunty so if you want to run it on your it on your macbook, you are going to run into them.

The first issue manifests itself as an error message stating “Error activating XKB configuration” everytime you log into X. This is related to xorg not apparently knowing what to do with the macbook keyboard variant – there is an umbrella bug ubuntu uses to capture all these types of errors you can find here. The solution is to modify your xorg.conf file to give X more information however I haven’t dinked around with this yet since its just a minor annoyance.

The issue with the trackpad just about renders the machine unusable for anything but the most simplist tasks however its luckily easy to fix (or at least mitigate). In Jaunty, just add a startup application that looks like this:

syndaemon -d

This will start up a program in the background every time you login that will disable the trackpad while you type, preventing it from causing the cursor to jump all over the screen. Syndaemon has a couple more options you can play with such as how long to disable the trackpad for but so far I’m finding the default settings to work ok.

A couple more random hints:

  • If you upgraded Jaunty and are now having problems with flash sites like Hulu telling you need flash 9 when you _know_ you had flash installed, try using synaptic to complete remove and then reinstall the flash-installer package – thats what it took for me  to get it working again on my living room media box.
  • If you are a quicksilver addict, check out Gnome Do – very slick and the keyboard shortcuts are even the same by default.

JavaBat

Posted in Tech on March 27th, 2009 by admin – Be the first to comment

Stumbled over another Java practice problem site for those poor bored individuals who just like writing code. This one is unique in that you are basically asked to implement a function which matches some specification you then write in a web form. On submit the site compiles the code and runs some unit tests against it to verify you have solved the particular challenge. You can create an account to track your progress if you like but its not mandatory. Pretty cool.

JavaBat

Project Euler

Posted in Tech on December 1st, 2008 by admin – Be the first to comment

I stumbled across Project Euler the other day and keep on finding myself going back for more. What is Project Euler you might ask? In their own words…

Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.

The motivation for starting Project Euler, and its continuation, is to provide a platform for the inquiring mind to delve into unfamiliar areas and learn new concepts in a fun and recreational context.

I’ve only done the first 4 problems so far so I haven’t even reached “level 1″ according to their scoring yet. Nothing has been all that challenging up to this point, the problems have been taking me around 30-60 minutes to solve. It’s just nice to have a pool of simple programming problems that one can go back to when one is in the mood, sort of like having a book of logic problems laying around.

MythTV woes

Posted in Tech on July 1st, 2006 by admin – Be the first to comment

I used to run Knoppmyth on my home theater PC since it made the setting up and installation of MythTV a breeze, especially on my old VIA EPIA box. I recently retired that box and decided get everything set up on a AMD64 based shuttle pc.
I wanted to go back to using a full blown distro since I use the box for a lot more than MythTV. I had good experience with Ubuntu in the past so grabbed the latest Dapper install iso and proceeded to get everything set up. I quickly ran into a complication – I wanted to reuse the mysql database from my previous installation so that my new box knew about what shows I had seen already and what I had recorded but had yet to watch. The version of KnoppMyth I was using was based on MythTV 19.0 but the only version in the Ubuntu repositories was 18.1 and the database schemas are not compatible. <sigh>
Looking over the Ubuntu forums I was able to find someone who had packaged up 19 and proceed to get everything installed just fine. Or at least it appeared, turns out the version I’m using has a problem where MythTV occasionally forgets about upcoming recordings. Restarting the backend solves the problem but I don’t want to force restart the backend every 30 minutes since it would create annoying gaps if I restarted the backend while I was recording something. I thought about jumping through all the hoops required to rebuild MythTV from scratch myself but I didn’t feel like going through the trouble right now so I came up with a short term hack.
I grabbed the latest version of mythweb and copied it into my apache2 root directory. Getting it working was as simple as creating a symlink to enable mod_rewrite and uncommenting a line in my PHP config to enable the mysql library. Once that was in place I wrote a simple script to wget the upcoming recordings, test to see if there was anything upcoming, and kick the backend if there wasn’t. I then hooked it into a cron job to run at 5 minutes before every half hour. Its a band aid that should hold me until they get 19 into the tree.
#!/bin/bash
wget -q -O- http://192.168.1.5/mythweb/tv/upcoming | grep -q list_separator
if [ $? -ne 0 ]; then
/etc/init.d/mythtv-backend restart
fi