Union-find data structure implementation in Scala with functional approach