I'm parametrizing a query in Scala.
I have an array of strings with column names named colNames
.
I want to create an string where for each name of the string the output is A.colName = B.colName
and then join all the items in the array putting an " AND " string between each item.
Example of input
val colNames = Array("colName1","colName2")
val table1 = "A"
val table2 = "B"
Example of the desired output
"A.colName1 = B.colName1 AND A.colName2 = B.colName2"
In a non FP language I would do that with a for loop, but I don't know how to do it in Scala in a functional way.
CodePudding user response:
You can use the map
and mkString
methods on Array:
scala> colNames map { colName => s"${table1}.${colName} = ${table2}.${colName}" } mkString " AND "
val res0: String = A.colName1 = B.colName1 AND A.colName2 = B.colName2