Montag, 17. März 2008

A closer look at Scala

So I have been reading more about Scala recently and it has been interesting reads so far.  As I consider myself a "Java-Refugee" I  was surprised to find this [1]. Scala surely has some different concepts some of them I want to write down here.


Methods
A few examples of method declarations:

//this method does nothingdef firstName() = {
 var back:String ="empty"
}

//this method doesn't do eitherdef firstName() = {
 var back:String ="empty"
 back = "still nothing"
}

//finally return the back variable
def firstName() = {
 var back:String ="empty"
 back
}

//declare the return type explicitly 
def firstName():String = {
 var back:String ="empty"
 back 
}
Note here in the last example that the last statement in the method is returned. It is also possible to use "return back" instead of just "back". The second example fails to return a value because the last line contains an assignment.
If you wanted to override a method in a subclass you have to declare it explicit via the override modifier.


Closures
Lastly a short example of Closures (=>), which offer a very nice alternative to anonymous inner classes in java:

val arr = Array(1, 2, 3, 4, 5)
val sum = arr.reduceRight((a:Int, b:Int) => a + b)


println(sum)
So reduceRight is a method which takes two values as parametes from the array(arr) and awaits a result originating from an operation involving these two parameters. So simply in this case, our result value sum will contain a sum over all elements of the value arr. 


Statics in Scala
There is no static keyword in Scala. Instead the concept of a Singleton is applied through the object keyword. If a class and an object have the same name the latter is described as companion object. 
Pattern matching
Pattern matching seems at a very first glance related to the switch statement in java, but there are so many differences and it turn out to be much more useful.   
Java:

int number= ..
String res="";

  switch (number) {
   case 1: res="ONE"; break;
   case 2: res="TWO"; break;
   case 3: res="THREE";  break;
   default: res=" i don't know";
 }

Scala:


val number=..
val res=""


res = i match{
   case 1 => "ONE"
   case 2 => "TWO"
   case 3 => "THREE"
   case _ => " i don't know"
}


Scala's case statement can't fall through, you don't have to use a break statement like in Java. The pattern matching is not restricted to primitives (int in java) you may use any type or object. 
Case classes
Note here that the underscore "_" has different meanings, so it should be used carefully and depending on the context in its.
Some Random thoughts pending:


Exceptions
Scala does not have the concept of checked exceptions. While this makes method declarations cleaner (IMHO), you have to take care of it either way. Since pattern matching works on types you can do nice things like this:


try {
..
} catch {
case e:SQLException => println(”Database error”)
case e:MalformedURLException => println(”Bad URL”)
case e:_ => println(”any other Error”)

}
Traits
Traits provide a mix-in mechanism which is similar to multiple inheritance but avoids some pitfalls like the diamond problem through a linearization mechanism. But still traits seem like a heavy topic since you have to think about initialization order. What I really like about traits is that you can attach or mix-in traits dynamically into an class:
trait funny { def saySome() = "hoho"}
class silent{}


val s= new silent() with funny
s.saySome //prints "hoho"


So you can put in Code in existing classes with extending them. I find this very interesting.
Left random Notes:




  • class names don't have to match filenames
  • While in Java you have only interfaces and classes in Scala you have 3 basic entities: classes, traits and objects
  • the constructor concept seems a little awkward since you write it directly in the class definition
  • imports can be scoped, this is pretty nice e.g. if you want to limit an important to an inner method 


Montag, 11. Februar 2008

First Steps in scala

Today I just met Scala. Scala is a programming language which runs on the JVM.
A first glance on the syntax via "Hello world":


object HelloWorld extends Application {
  println("Hello, world!")
}


object HelloWorld {
  def main(args: Array[String]) =
  println("Hello, world!")
}

Samstag, 20. Oktober 2007

XML API's

The two most common XML API's are DOM and SAX. Now there is a third 'major' API evolving: StAX.


Concepts
Push - SAX
The API 'pushes' data to the application. The data is sequentially processed.
Pull - DOM
The API offers to 'pull' data out. Full random access to the data is available.


SAX uses a hierarchical event handler and DOM uses a nested iterators. Finally StAX uses a hierarchical iterator. So while SAX is very fast and efficient the API is a little bit difficult to use. You have to maintain  where you are in your document yourself on the other hand an iterator gives you a common control abstraction. In most cases you don't need access to the complete XML data tree but to a distinct subset and right here StAX fits in. 
So I think this illustrates pretty nicely that it's not only important to look at the 'speed' of your XML processing application but also about maintainability and extensibility. If it's really hard for you to implement your solution with SAX and performance is not a problem StAX  is a nice alternative.  
This is the first time where I realized what huge impact the design of an API has on code. Or to be more precise, on the actual result of what you had intended to code. 

Dienstag, 13. März 2007

Guice - Dependency Injection

Today this was released:
http://code.google.com/p/google-guice/
Minimizing dependencies in OOP projects sounds nice. Spring uses XML files for configuration while guice uses Java 5 Annotations. So far i only have experience with picocontainer and it's nice and simple to use.
further read

Freitag, 9. März 2007

trac(ing)

Today i stumbled across this:
http://trac.edgewall.org/
This seems like a really good idea for project management since it integrates a lot of features that do seem use full in that context like:  a wiki or timeline features.

Montag, 20. November 2006

Sorting algorithms

Wikipedia (as usual) has very nice material on this[1]. Here are also some[2][3] graphical representations of sorting algorithims.

EDIT 2010: found [4] this on topic, you never know when there is a new search algorithm appearing.  

[1]http://en.wikipedia.org/wiki/Sorting_algorithm
[2]http://www.cs.rit.edu/~atk/Java/Sorting/sorting.html
[3]http://cg.scs.carleton.ca/~morin/misc/sortalg/
[4]http://www.drmaciver.com/2010/01/understanding-timsort-1adaptive-mergesort/

Mittwoch, 25. Oktober 2006

Webservices - Ménage à trois

Here I want to write down a little reminder to myself about current Webservices techniques.

RPC - Remote Procedure Call
Uses XML encoded Data types for communication.


WSDL - Webservices
So this is powering all the SOA(P?!) Hype. I have only some limited experience with some WSDL Webservices build using .NET 1.0 / 2.0.
Visual Studio holds your hand very nicely (as expected) for simple stuff. But to deal with more complex and custom Objects it would be very interesting i guess. I tried a "quick" example to get a Java client to interact with a .NET Webservice and it failed miserably.


REST - Representational State Transfer
Usually it goes like this: You have some kind of data and then you think about API's, how to access or manipulate this data. REST turns things a little upside down. There is a well defined set of operations : GET, PUT, DELETE. The concept is focused on the interaction of state full resources.


It will be interesting to see how these will develop in the future.