Scala flatten List

后端 未结 5 1730
無奈伤痛
無奈伤痛 2020-12-29 11:13

I want to write a function that flats a List.

object Flat {
  def flatten[T](list: List[T]): List[T] = list match {
    case Nil => Nil
    case head :: N         


        
5条回答
  •  执念已碎
    2020-12-29 11:34

    If someone does not understand this line of the accepted solution, or did not know that you can annotate a pattern with a type:

    case (head: List[_]) :: tail => flatten(head) ++ flatten(tail)
    

    Then look at an equivalent without the type annotation:

    case (y :: ys) :: tail => flatten3(y :: ys) ::: flatten3(tail)
    case Nil :: tail => flatten3(tail)
    

    So, just for better understanding some alternatives:

    def flatten2(xs: List[Any]): List[Any] = xs match {
      case x :: xs => x match {
        case y :: ys => flatten2(y :: ys) ::: flatten2(xs)
        case Nil => flatten2(xs)
        case _ => x :: flatten2(xs)
      }
      case x => x
    }
    
    def flatten3(xs: List[Any]): List[Any] = xs match {
      case Nil => Nil
      case (y :: ys) :: zs => flatten3(y :: ys) ::: flatten3(zs)
      case Nil :: ys => flatten3(ys)
      case y :: ys => y :: flatten3(ys)
    }
    
    val yss = List(List(1,2,3), List(), List(List(1,2,3), List(List(4,5,6))))
    flatten2(yss) // res2: List[Any] = List(1, 2, 3, 1, 2, 3, 4, 5, 6) 
    flatten3(yss) // res2: List[Any] = List(1, 2, 3, 1, 2, 3, 4, 5, 6) 
    

    By the way, the second posted answer will do the following, which you probably don't want.

    val yss = List(List(1,2,3), List(), List(List(1,2,3), List(List(4,5,6))))
    flatten(yss) // res1: List[Any] = List(1, 2, 3, List(), 1, 2, 3, 4, 5, 6) 
    

提交回复
热议问题