Tag Archives: scala

Inne

Roman Numerals Converter Code Kata summary

Brow­sing thro­ugh GitHub i came across XSolve Code Kata repo­si­tory. Code Kata #1 have two imple­men­ta­tions (PHP, C#) of Roman nume­rals conver­ter. I had some free time and wanted to imple­ment Scala version. The result is on my GitHub.

I added Roman-Arabic conver­sion compa­red to XSolve solu­tion. Project has full test cove­rage, what means every single line of code was execu­ted at least once (love that fancy Travis and Cove­ralls badges).

What is being used:

  • case clas­ses ArabicNum and RomanNum, of course it could be int and string, but hey, Scala has rich typesystem ;-)
  • whole NumConverter class is single­ton object
  • one pattern matching method
  • break() func­tions, which I find funny that Scala doesn’t have imple­men­ted such base things natively
  • FlatSpec from Scala­Test suite (BDD approach)

Be warned, vali­da­tion func­tion is ugly, conta­ins nested IFs and needs refac­to­ring, but as long as it passes tests it’s OK.

Whole code is not very „Scalish”. I consi­der myself as an Advan­ced Begin­ner on Scala accor­ding to Drey­fus Model and I don’t care ;-)

Inne

Computing file hashes using Scala

I thought it would be nice to enter 2014 with some new know­ledge. I couldn’t survive over 2 weeks without coding so I deci­ded to take a closer look at Scala. Since I’m rather inter­me­diate Java coder, I thought it would be great to try another JVM language.

Java casts me away because of too much boiler­plate code. I feel so stupid being forced to type stuff like final BufferedString bufStr = new BufferedString(); Is Java compi­ler really that stupid that can’t figure out what type is bufStr? It seems that some­body thought the same like me and made compi­ler more smart.

Of course Scala is not about syntax only. It repre­sents a whole diffe­rent appro­ach to program­ming. Scala uses func­tio­nal para­digms instead of impe­ra­tive. After 3 days of inten­sive API reading I’ve prepa­red a little code snip­pet to illu­strate key concepts of Scala.

Gist conta­ins HashGenerator single­ton object that can compute SHA-256 hash string from file contents. It uses two redun­dant methods to get hash. getSha1HashUsingFuncChain() is more „scalish”, getSha1HashUsingMatcher() uses matcher, a power­ful Scala feature.

Accor­ding to my runtime measu­re­ment, func­tion using matcher is about 10% faster on my machine. Skim the commen­ted source below: