I thought it would be nice to enter 2014 with some new knowledge. I couldn’t survive over 2 weeks without coding so I decided to take a closer look at Scala. Since I’m rather intermediate Java coder, I thought it would be great to try another JVM language.
Java casts me away because of too much boilerplate code. I feel so stupid being forced to type stuff like
final BufferedString bufStr = new BufferedString(); Is Java compiler really that stupid that can’t figure out what type is bufStr? It seems that somebody thought the same like me and made compiler more smart.
Of course Scala is not about syntax only. It represents a whole different approach to programming. Scala uses functional paradigms instead of imperative. After 3 days of intensive API reading I’ve prepared a little code snippet to illustrate key concepts of Scala.
HashGenerator singleton object that can compute SHA-256 hash string from file contents. It uses two redundant methods to get hash.
getSha1HashUsingFuncChain() is more „scalish”,
getSha1HashUsingMatcher() uses matcher, a powerful Scala feature.
According to my runtime measurement, function using matcher is about 10% faster on my machine. Skim the commented source below: