简体   繁体   中英

Taking union of possibly empty list of data frames

I have the following code:

val dataFrames: List[DataFrame] = [...]
// TODO There has to be a better way to do lines below.
val salesOrderDF: Option[sql.DataFrame] =
  if (dataFrames.length > 1) {
    Some(dataFrames.reduceRight(_.union(_)))
  } else if (dataFrames.length == 1) {
    Some(dataFrames.head)
  } else {
    None
  }

Is there a better way to do this? It seems like the if and the else if cases can somehow be combined.

You don't need the else if , a reduce with a single element will just return that element. If you don't want to use an if-else, you can check if the list is empty with a pattern match.

val salesOrderDF: Option[sql.DataFrame] = dataFrames match {
    case Nil => None
    case nonEmptyDfs => Some(nonEmptyDfs.reduce(_ union _))
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM