Scala implementation without mutability