We have a list of strings and i grouping them by below program.
input: val k = List("a", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e", "e")
output: *List(List(a, a, a, a), List(b), List(c, c), List(a, a), List(d), List(e, e, e, e))*
Program:
def pack(ls:List[String]):List[List[String]]={
val (a,next) = ls span {_ == ls.head}
if ((next) == Nil) List(a)
else a :: pack(next)
}
However when i do a List cons operators, i get the output as mentioned below.
Input:
val a =List("a", "a", "a", "a")
val b = List ("b")
val c = List ("c", "c" )
val a1 = List("a", "a")
val d = List("d")
val e = List( "e", "e", "e", "e")
*List(a::b::c::a1::d::e)*
output:
*List(List(List(a, a, a, a), List(b), List(c, c), List(a, a), List(d), e, e, e, e))*
Is there any way i can the output as below in a single command in scala?
*List(List(a, a, a, a), List(b), List(c, c), List(a, a), List(d), List(e, e, e, e))*