Please advice on algorithm and implementation to compare elements in a very long list in Scala. I have a list with thousands of strings (from SQL) and I need to compare each list element with all other elements in this list.
As a result I need to get a list of tuples: List[(String, String, Boolean)]
where first two elements are strings to match and third is a result.
For a list of N elements my algorithm so far is as follows:
- Take head of the list
- Compare head with remaining N-1 elements in the list
- Make new list from a tail of the old list and do all above work with this new list of N -1 elements:
Code:
/**
* Compare head of the list with each remaining element in this list
*/
def cmpel(
fst: String, lst: List[String],
result: List[(String, String, Boolean)]): List[(String, String, Boolean)] = {
lst match {
case next :: tail => cmpel(fst, tail, (fst, next, fst == next) :: result)
case nill => result.reverse
}
}
/**
* Compare list elements in all combinations of two
*/
def cmpAll(lst: List[String],
result: List[(String, String, Boolean)]): List[(String, String, Boolean)] = {
lst match {
case head :: tail => cmpAll(tail, result ++ cmpel(head, tail, List()))
case nill => result
}
}
def main(args: Array[String]): Unit = {
val lst = List[String]("a", "b", "b", "a")
println(cmpAll(lst, List()))
}
Result:
List((a,b,false), (a,b,false), (a,a,true), (b,b,true), (b,a,false), (b,a,false))
Thanks!