Flattening nested lists of the same type

2019-04-11 02:09发布

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?

3条回答
混吃等死
2楼-- · 2019-04-11 02:40

In an ideal world, where the wretched type erasure would not exist, you would do something like this:

// WON'T WORK
def flatten[T <: SpecialList](parentList: T): List[Exp] = parentList.elements flatMap {
    case x:T => x.elements
    case somethingElse => List(somethingElse)
}

But the best solution under the circumstances is, in my opinion, this one:

def flatten[T <: SpecialList](parentList: T): List[Exp] = parentList.elements flatMap {
    case x:SpecialList if x.getClass == parentList.getClass => x.elements
    case somethingElse => List(somethingElse)
}

It's a bit more generic than the one proposed in the question since you don't need to bother whether the argument is a ListA or ListB and it will also work if in the future you'll add a ListC.

However, this won't solve your more general problem for flattening at arbitrary depths, since flatten(ListA(...)) must also return a ListA(...) in the end - in the case above it returns a List which looses it's initial meaning. A solution to this problem could be:

abstract class SpecialList {
    val elements: List[Exp]

    def flatten: SpecialList = createAnother(elements flatMap {
        case x: SpecialList => {
            val flattenX = x.flatten
            if (flattenX.getClass == this.getClass) flattenX.elements else List(flattenX)
        }
        case somethingElse => List(somethingElse)
    })

    // Creates another special list of the same type
    def createAnother(elements: List[Exp]): SpecialList

}


case class ListA(elements: List[Exp]) extends SpecialList {
    override def toString: String = "ListA("+elements.mkString(",")+")"

    def createAnother(elements: List[Exp]) = ListA(elements)
}

case class ListB(elements: List[Exp]) extends SpecialList {
    override def toString: String = "ListB("+elements.mkString(",")+")"

    def createAnother(elements: List[Exp]) = ListB(elements)
}

The problem in this case is that the createAnother bit is pure boilerplate. On the other hand, this version maintains the generality of the above solution.

A third suggestion, which may involve refactoring your code a bit more is to drop the ListA and ListB types altogether, since it seems to me that their purpose is to provide a tag to a list of Exp. So consider this solution:

case class SpecialList(tag: Tag, elements: List[Exp]) extends Exp {
    def flatten: SpecialList = {
        val newElems = elements flatMap {
            case x: SpecialList => {
                val flattenX = x.flatten
                if (flattenX.tag == this.tag) flattenX.elements else List(flattenX)
            }
            case somethingElse => List(somethingElse)
        }
        SpecialList(tag, newElems)
    }

    override def toString = tag.toString ++ "(" + elements.mkString(",") + ")"

}


sealed abstract class Tag {

    // Syntactic sugar to maintain the notation used in the question
    def apply(elements: Exp*): SpecialList = SpecialList(this, elements.toList)

}

object ListA extends Tag { override val toString = "ListA" }
object ListB extends Tag { override val toString = "ListB" }

From a syntactic point of view, it's pretty much the same, since you have

val x = ListA(Element(A), Element(B), ListA(Element(C), Element(D)), ListB(Element(E),Element(F), ListA(Element(C), ListA(Element(D)))))
x.flatten => ListA(Element(A),Element(B),Element(C),Element(D),ListB(Element(E),Element(F),ListA(Element(C),Element(D))))

This may not fit your problem, however, so sorry if I went off the rails a bit there.

查看更多
放我归山
3楼-- · 2019-04-11 02:44

I prefere a recursive way. In general I would do something like this:

def flattenList[A](l: List[Any]): List[A] = {
  var acc = List[A]()
  l foreach ( entry => entry match {
    case a: List[Any] => acc = acc ::: flattenList(a)
    case b: A => acc = acc :+ b
  })
  acc
}

This will flatten you a List(Element(A), Element(B), List(Element(C), Element(D), List(Element(E), Element(F)))) to List(Element(A), Element(B), Element(C), Element(D), Element(E), Element(F))

查看更多
Juvenile、少年°
4楼-- · 2019-04-11 02:56

A true functional way. Without using a variable.

def flatten[A](list: List[A]): List[A] = list match {
   case Nil => Nil
   case (ls: List[A]) :: tail => flatten(ls) ::: flatten(tail)
   case h :: tail => h :: flatten(tail)
}
查看更多
登录 后发表回答