Algorithms in functional Scala