I am trying to find the intersection of two lines in Scala order. I am new to Scala, but it seems to me that this should be single-line. I tried using both the map and foldLeft, and have not yet received the correct answer.
Given two lines, return a list of characters that will be the same in order. For example, "abcd", "acc" should return "a", and "abcd", "abc" should return "abc".
Here are two functions that I have tried so far
(str1 zip str2).map{ case(a, b) => if (a == b) a else ""}
and
(str1 zip str2).foldLeft(""){case(acc,n) => if (n._1 == n._2) acc+n._1.toString else ""}
I want to do something like this
(str1 zip str2).map{ case(a, b) => if (a == b) a else break}
but it does not work.
I know that I can do this with multiple lines and a for loop, but it looks like a single liner. Can anyone help?
thank