Posts Tagged ‘Java’

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.

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.