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:

Podobne wpisy:

  1. Play­ing with PHP func­tio­nal way
  2. Mój pierw­szy raz… z Javą :-)
  3. O peha­powcu, który chciał zostać javowcem
  4. Infor­ma­tyczne lektury do poduchy

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany.

Możesz użyć następujących tagów oraz atrybutów HTML-a: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <p> <pre lang="" line="" escaped=""> <q cite=""> <strike> <strong>