Scala Algorithms

By William Narmontas

The Scala programming language has a great wealth of possibilities that are often unexplored. It is not enough to simply take a piece of Java code, translate it to Scala syntax and then compare the two.

Scala offers new ways to think about problems, especially in terms of immutability. No longer do we live in an age where immutability is costly - we live in the age of powerful Java Virtual Machines like GraalVM which can turn immutable code into mutable under the surface, and we gain in readability, testability and predictability.

Here, I provide a variety of code examples written in a purely functional and/or immutable style of Scala.

True Scala Algorithms

Learn to think in Scala:

  • Immutable "True Scala" pure-functional style
  • With proofs and explanations
  • With real test-cases that you can run

Directory

I provide this as a resource to learn Scala to an advanced level, especially in preparation for job interviews. Explanations are always provided, but some solutions are paid.

  1. Balanced parentheses algorithm in immutable/pure functional Scala with tail call recursion optimisation
  2. Find maximum potential profit from an array of stock prices - pure-functional immutable Scala solution
  3. Count divisors algorithm in immutable/pure functional Scala
  4. Count number of changes (manipulations) needed to make an anagram in immutable/pure functional Scala with foldLeft and a MultiSet