I want to conditionally merge elements following eachother in a list based on some expression. An example to better explain what I would like to do:
from:
val list = List("a1", "a2", "b1", "b2", "b3", "a3", "a4")
I would like to merge all elements starting with b into a single element to get a resulting list like this:
List("a1", "a2", "b1-b2-b3", "a3", "a4")
In my use case, the b-elements always follow in sequence but the number of b-elements can vary from no elements to tens of elements.
I've tried doing something like
list.foldLeft("")((s1, s2) => if (s1.matches("""b\d""") && s2.matches("""b\d""")) s1 + "-" + s2 else s1)
but it doesn't render me anything useful.
Any suggestions on how to approach this?