简体   繁体   中英

Scala function does not return a value

I think I understand the rules of implicit returns but I can't figure out why splithead is not being set. This code is run via

val m = new TaxiModel(sc, file) 

and then I expect

m.splithead

to give me an array strings. Note head is an array of strings.

import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD

class TaxiModel(sc: SparkContext, dat: String) {
  val rawData = sc.textFile(dat)

  val head = rawData.take(10)
  val splithead = head.slice(1,11).foreach(splitData)

  def splitData(dat: String): Array[String] = {
    val splits = dat.split("\",\"")
    val split0 = splits(0).substring(1, splits(0).length)
    val split8 = splits(8).substring(0, splits(8).length - 1)
    Array(split0).union(splits.slice(1, 8)).union(Array(split8))
  }
}

foreach just evaluates expression, and do not collect any data while iterating. You probably need map or flatMap (see docs here )

head.slice(1,11).map(splitData) // gives you Array[Array[String]]

head.slice(1,11).flatMap(splitData) // gives you Array[String]

Consider also a for comprehension (which desugars in this case into flatMap ),

for (s <- head.slice(1,11)) yield splitData(s)

Note also that Scala strings are equipped with ordered collections methods, thus

splits(0).substring(1, splits(0).length)

proves equivalent to any of the following

splits(0).drop(1)
splits(0).tail

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