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

Thursday, December 10, 2009

Prime Factors Kata in Scala

After practicing the Prime Factors Kata in Ruby a couple of days, I tried the same exercise in Scala.

Except the fact that practicing Katas is a wonderful tool to learn your IDE and optimize your workflow, I think Katas are also great to learn programming languages. You just don't have to think about your programming-problem, you can just think about your language.
Its been a while since I played around with Scala, so i started my daily Kata in Scala to refresh my skills.

The fact that Scala is not just an object oriented language but also a functional language, led me to a complete different solution of the prime factors kata as in Java or Ruby. I took the immutable List of Scala instead of a mutable Array, so I had to build the algorithm in a recursive manner.
Ok, enough talk. Here is my first solution:

// PrimeFactorsTest.scala
import org.junit.Test;
import org.junit.Assert._;
import org.hamcrest.CoreMatchers._;


class PrimeFactorsTest {
 @Test
 def shouldFactorNumbers = {
  Map(
   1  -> Nil,
   2  -> List(2),
   3  -> List(3),
   4  -> List(2,2),
   5  -> List(5),
   6  -> List(2,3),
   7  -> List(7),
   8  -> List(2,2,2),
   9  -> List(3,3),
   10 -> List(2,5),
   (2*2*5*7*13) -> List(2,2,5,7,13)
   
  ).foreach{ case (n, factors) => 
   assertThat(PrimeFactors.of(n), is(equalTo(factors)))
  }
 }
}
// PrimeFactors.scala
object PrimeFactors {

 def of(n:Int) : List[Int] = tryFactor(2, n)
 
 private def tryFactor(divider: Int, n: Int) : List[Int] = 
  if (n > 1) 
   takeIfFactorElseTryNext(divider, n) 
  else 
   Nil
 
 private def takeIfFactorElseTryNext(divider: Int, n: Int) : List[Int] = 
  if (n % divider == 0) 
   divider :: takeIfFactorElseTryNext(divider, n/divider) 
  else 
   tryFactor(divider+1, n)
}

EDIT:
A few days later, I came to a slightly better solution. I now use a nested function tryFactor to do the recursion. It's nested because in a different context it makes absolutly no sense.
As in my first solution I used object instead of class to create the singleton object PrimeFactors. Again it is not written in tail-recursive style, but Scala obviously optimizes it.

// PrimeFactors.scala
object PrimeFactors {
 
 def of(n:Int) = {
  def tryFactor(n:Int, divider:Int) : List[Int]  =
   if (n == 1) Nil
   else if (n % divider == 0) divider :: tryFactor(n/divider, divider) 
   else tryFactor(n, divider+1)
  
  tryFactor(n, 2)
 }
}

Tuesday, September 1, 2009

Pattern to low couple to proprietary frameworks

Most Java programmers that are using proprietary libraries or other immutable frameworks are faced with a problem: Every time you need some functionality to work with a framework class you build procedural or even worse redundant code. This leads to high coupling between the framework and the rest of the application.

Here are some thoughts to solve this problem:

Inheritance
The additional functionality is added to an inherited class. This often leads to many casts and instanceof in the source code.

Simple Wrapper
A class that holds the class of the framework. All additonal functionality is added by the wrapper class. Whenever the framework class is needed it can be retrieved by a getter of the wrapper class.

Decorator
Using the decorator pattern often is not possible. The reason is that the framework class has to implement a shared interface with the decorater class but there is no way to change the framework. If the framework class does not implement such an interface the decorator pattern can not be used.

Monkey Patching
For Java-Programmers this is unusual, but often used in ruby and other dynamic languages. With the rise of JRuby and Groovy you can now patch your Java framework classes easily and follow TDA to get DRY code. In some static languages like Scala this is possible, too (see Pimp my Library).
In my opinion monkey patching is the best way to work with unchangable frameworks.