New Scala dev here. Is there a more idiomatic or efficient way to accomplish this in Scala?
Given a list of the following strings, I need to obtain the unique 'main parts' once any parts including and after a "-" have been removed.
The output should be a list of sorted strings. Also note both ";" and "," have been used as separators.
Input:
val data: List[String] = List(
"W22; O21; B112-WX00BK; G211; B112-WI00BK; G11",
"W22; K122l; B112-WI00BK; O21; B112-WX00BK; G211",
"W21, V32",
"W21, N722",
"S133-VU3150; S133-SU3150; R22-VK3150; R123-VH3"
)
Desired Output:
List(
B112 G11 G211 O21 W22,
B112 G211 K122l O21 W22,
V32 W21,
N722 W21,
R123 R22 S133
)
My solution:
def process(input: String): String =
input.split(" ").map(word => {
if (word contains "-")
word.take(word.indexOf("-"))
else word
.replace(";", "").replace(",","")}).toSet.toList.sorted.mkString(" ")
val result: List[String] = data.map(process(_))
data.map(process)
is sufficient. \$\endgroup\$