Let’s say I want to flatten nested lists of the same type… For example
ListA(Element(A), Element(B), ListA(Element(C), Element(D)), ListB(Element(E),Element(F)))
ListA contains nested list of the same type (ListA(Element(C), Element(D))) so I want to substitute it with the values it contains, so the result of the upper example should look like this:
ListA(Element(A), Element(B), Element(C), Element(D), ListB(Element(E),Element(F)))
Current class hierarchy:
abstract class SpecialList() extends Exp {
val elements: List[Exp]
}
case class Element(name: String) extends Exp
case class ListA(elements: List[Exp]) extends SpecialList {
override def toString(): String = "ListA("+elements.mkString(",")+")"
}
case class ListB(elements: List[Exp]) extends SpecialList {
override def toString(): String = "ListB("+elements.mkString(",")+")"
}
object ListA{def apply(elements: Exp*):ListA = ListA(elements.toList)}
object ListB{def apply(elements: Exp*):ListB = ListB(elements.toList)}
I have made three solutions that works, but I think there have to be better way to achieve this:
First solution:
def flatten[T <: SpecialList](parentList: T): List[Exp] = {
val buf = new ListBuffer[Exp]
for (feature <- parentList.elements) feature match {
case listA:ListA if parentList.isInstanceOf[ListA] => buf ++= listA.elements
case listB:ListB if parentList.isInstanceOf[ListB] => buf ++= listB.elements
case _ => buf += feature
}
buf.toList
}
Second solution:
def flatten[T <: SpecialList](parentList: T): List[Exp] = {
val buf = new ListBuffer[Exp]
parentList match {
case listA:ListA => for (elem <- listA.elements) elem match {
case listOfTypeA:ListA => buf ++= listOfTypeA.elements
case _ => buf += elem
}
case listB:ListB => for (elem <- listB.elements) elem match {
case listOfTypeB:ListB => buf ++= listOfTypeB.elements
case _ => buf += elem
}
}
buf.toList
}
Third solution
def flatten[T <: SpecialList](parentList: T): List[Exp] = parentList.elements flatMap {
case listA:ListA if parentList.isInstanceOf[ListA] => listA.elements
case listB:ListB if parentList.isInstanceOf[ListB] => listB.elements
case other => List(other)
}
My question is whether there is any better, more generic way to achieve same functionality as in all of upper three solutions there is repetition of code?
A true functional way. Without using a variable.