Fraction reduction in Scala (first contact)

After a failed attempt to grasp Haskell, but somewhat seduced by the concise and elegant ways of Functional Programming, I’ve started to look for alternatives . Knowing Java, and having an everyday interaction with the JVM the obvious choices were Clojure or Scala . I never had the chance to try Clojure, probably because right now I enjoy Scala so much (the butterflies…) . Of course I cannot say that Scala scales, or if Scala is the next Java, or Scala is the answer, but from what I’ve experienced so far, things look good and I am optimistic that some day Scala will receive the attention it deserves .

There are a lot good books on Scala, but probably the most popular is “Programming in Scala, A comprehensive step-by-step guide” by Martin Odersky (Scala’s creator), Lex Spoon, and Bill Venners .

In a chapter there, the authors give us an insight on how to create Functional Objects -> objects that are immutable . The materialization of the concept is a Rational class, that encapsulates the idea of rational numbers . It allows us to be add, subtract, multiply, compare etc. rational numbers .  At some point in the example the fraction is reduced to the lowest terms.

Reducing the fraction to its lowest terms  (also known as normalization) is probably one of the simplest programming exercises we all start with . The actual problem is to determine the greatest common divisor of the fraction’s denominator and numerator, and divide those numbers by it . Not long ago I’ve written a solution for this exercise in C, but we all know C is far from being an elegant peace of technology.

Inspired (a lot!) by the example in the book, I’ve come with this solution:



To compile and run the above we can use scalac (the equivalent of javac) as:

Or we can use fsc which is the Scala compiler daemon . Usually compilation with scalac has a certain delay as the jvm needs to initialize itself, scan the .jar contents of the scala distribution you have installed, etc . So fsc will create a local server (has a port attached to itself) . The server will remain open so any subsequent compilation won’t require a reinitialization . Except the inner cuisine, fsc is very similar to scalac, so the compilation syntax is the same:

Now let’s focus on some explanations . I am sure the syntax is somehow familiar but you probably noticed that:

  • Apparently in the Fraction.scala there’s no constructor . Actually there’s one, as classes can take input parameters directly ;
  • Input validations (checked preconditions) can be done using require . If the preconditions fails, an exception is thrown (IllegalArgumentException) ;
  • When we override a method (in our case toString()), we need to explicitly use the override keyword, otherwise the class won’t compile ;
  • In Scala semicolons(;) are in the majority of cases optional . (You need to write a semicolon when you have multiple expressions sharing the same line of code) ;
  • If a function doesn’t have any input parameters (eg. toString()) there’s no need to write the brackets ;
  • There’s a difference between a val and var . In our case numerator and denominator are read-only kind of fields, as they are defined using a val instead of var, so the object cannot change, thus the immutability .
  • Scala has type inference so in the majority of cases you won’t need to write redundant constructs .
  • MainObject is a singleton object . From what I understand, Scala doesn’t support static functions inside classes, so this construct “singleton object” is preferable instead of the classical approach .

The above example  was very simple, and doesn’t proves much of the functional power of Scala. It’s just a small glimpse of the syntax, and the fact that a Java programmer, can start small, and later adapt his style to become more functional-oriented .

Right now I am giving Scala a chance, and I am almost sure I’ll be satisfied .

Leave a Reply

Your email address will not be published. Required fields are marked *

Are we human, or are we dancer *