Showing posts with label scala. Show all posts
Showing posts with label scala. Show all posts

Wednesday, August 19, 2009

Knight's Tour in Scala

I've been doing some experimentation with Scala lately, mostly by reading the Wampler / Payne Programming Scala in O'Reilly Labs. Learning a language by reading about it is deceptive, so I've been looking for an excuse to write some non-trivial Scala code with which to better exercise and test my knowledge of the language.


After skimming the Daily WTF's Praxis for Automating the Knight's Tour, it seemed like a tractable problem that would be fun to code up in Scala, so I proceeded to do just that. It's a brute-force solution to the problem, and I'm not yet sure how idiomatic my Scala code is, but it does, at least, solve the problem:


class KnightsTour(val dimensions : Pair[int,int], val startPosition : Pair[int,int] ) {

val relativeMoves = List( Pair(2,1), Pair(2,-1), Pair(-2,1), Pair(-2,-1), Pair(1,2), Pair(-1,2), Pair(1,-2), Pair(-1,-2) )
val xPositions = 0 until dimensions._1
val yPositions = 0 until dimensions._2
val tourLength = dimensions._1 * dimensions._2;
var closedTours = 0;
var openTours = 0;

def printPaths() : Unit = {
printChildPaths( List(startPosition) )
println( "There were " + ( openTours + closedTours ) + " tours (" + openTours + " open, " + closedTours + " closed)." );
}

def printChildPaths( path : List[ Pair[int,int] ] ) : Unit = {
val moves = getMoves( path.first );

if( path.size == tourLength ) {
if( moves.contains(startPosition) ) {
closedTours += 1
println( "Found a closed tour: " + path )
} else {
openTours += 1
println( "Found an open tour: " + path )
}
} else {
moves.filter( move => !path.contains(move) ).foreach( move => printChildPaths( move :: path ) )
}
}

def getMoves( position: Pair[int,int] ) : List[Pair[int,int]] = {
relativeMoves.map( move => Pair(position._1 + move._1, position._2 + move._2 ) ).filter( withinDimensions )
}

def withinDimensions( position: Pair[int,int] ) : boolean = {
return xPositions.contains( position._1 ) && yPositions.contains( position._2 )
}

}

val tour = new KnightsTour( Pair(3,4), Pair(0,0) )
tour.printPaths();


All in all, I'm pleased with it, although I'm sure i'll read this again later and wince at my early Scala code. Comments welcome.

Thursday, September 4, 2008

Scala in Eclipse

Last time I looked at Scala, I quickly stumbled over the nascent support for Scala in Eclipse and found that I didn't have the energy to work with a JVM language that didn't have good IDE support.  It's been a while since then, so it seemed like a good time to look at Scala again and see how support for Scala in Eclipse has improved.


Installing Scala in Eclipse
Installing Scala in Eclipse was pretty easy.  There's a documentation page covering it, Eclipse 3.4 (Ganymede) is supported, so I added the update site, installed, restarted Eclipse, and voila, a Scala perspective, as shown on the Scala site:

Hello, World
Following along with either the Eclipse/Scala plugin page or with the default tutorial, I tried creating my first Scala "Hello World", and immediately started hitting minor hiccups again.  

First of all, starting strings often highlights the open quote with an error indicator that doesn't go away when you close the string.

Secondly, there's no shortcut for running a Scala application.  Alt-Shift-X brings up the usual menu with no addition for Scala.  Likewise, you can't "Run As > Scala Application", another normal way to run an application in the Java development tools.  Your only option is to go into the run configurations dialog, create a new run configuration under Scala Applications and enter the class to be used.

Further confusion sets in within the Run Configuration dialog, where there's a Browse button to search for a main class, but the HelloWorld object I've created doesn't show up in the dialog that results from pressing this button.  As a result, if I type in the fully-qualified name of the object, the run configuration works, but it's fairly painful to reach that stage.

Interaction with Java
Continuing with the tutorial, I start the stage of interacting with Java classes.  Although the typed-out examples show import statements, I'm used to these import statements being generated by the IDE in Java, so I decide to omit them.   The Date type and getDateInstance methods are highlighted as being not found, although the warning information that appears when you hover over the error is both simpler and uglier than the Java equivalents, and incorrectly sized for the text, so scrollbars appear.  There are no quick fixes available, so it seems likely that I'm going to have to enter the imports by hand.  I do seem to get autocompletion on the imports, which makes entering these less painful than they might have been.

Having completed the import statements, the Scala editor is now aware of the Date instance, but seems to have failed to notice that getDateInstance() is overloaded; it's complaining that I'm offering up (int.Locale) to getDateInstance().  The code still runs when the run configuration is executed, but the editor is blissfully unaware of the successful compilation of the code, and continues to present the getDateInstance() method as being in error, even when I take it out and put it back in.

Second First Impressions
Well, this is my second time checking out Scala on Eclipse, and my first impressions this time around is that there's still a ways to go before Scala in Eclipse approaches the polish I'd like to see.  It's certainly better than when I last looked, but I'd still be tempted to take a look at the support in IDEA and Netbeans before sticking with Scala in Eclipse.

Wednesday, May 7, 2008

Scala Lift-Off

Interesting to see Scala and Lift gain a little momentum, with Scala Lift-Off, an unconference.