Can I use "_" here?

scala> rdd2.take(5)
res3: Array[String] = Array(As, software, developers,, we’ve, all)

so:

scala> rdd2.map( (_,1) )

is similar to:

rdd2.map(x => (x,1) )

Should I use the first map() or the second map() above?
when dealing with “_” I am always unsure if there was an error.

Thank you.

There is no objectively correct answer to this. It’s mainly a question of whether the code is likely to be reasonably clear to someone reading it.

3 Likes
/** Lazily instantiated singleton instance of SparkSession */
object SparkSessionSingleton {

  @transient  private var instance: SparkSession = _

  def getInstance(sparkConf: SparkConf): SparkSession = {
    if (instance == null) {
      instance = SparkSession
        .builder
        .config(sparkConf)
        .getOrCreate()
    }
    instance
  }
}

for the code above, what does the “_” mean for this statement? :slight_smile:

@transient  private var instance: SparkSession = _

In this case, the underscore is used to initialize instance to the “default value”, which is null for reference types like SparkSession.
Scala 2 has a lot of unrelated usages of underscores, which can be pretty confusing (which is why Scala 3 replaces some of them). See e.g. https://www.baeldung.com/scala/underscore for a list of the various meanings.

2 Likes

Then just don’t use _. (At least for a while, until you become more experienced in the language, and you get a better understanding of type inference and the compiler.)

When using _, depending on the situation, sometimes the compiler can infer the types of the parameters in the function literal, and sometimes it cannot (because it’s ambiguous, or there isn’t enough information to infer it, or there are too many parameters, etc.).

The _ is syntactic sugar to make code more concise, but there is nothing wrong with using the non-sugar function literals. When in doubt, stay away from sugar and help the compiler as much as you can!

3 Likes

There is no objectively correct answer to this.

Correct. The only reason to use placeholder syntax is if it makes your team happy for some reason. It’s purely a matter of preference and has no impact at all on runtime behavior.

1 Like

Perhaps the question is should you use tuple syntax here.

scala> List(42).map(_ -> "x")
val res0: List[(Int, String)] = List((42,x))

scala> List(42).map((_, "x"))
val res1: List[(Int, String)] = List((42,x))

People enjoy all sorts of concision.

scala> List(42).map(1.+)
val res2: List[Int] = List(43)

scala> List(42).map(_ + 1)
val res3: List[Int] = List(43)

I think you’re only obligated to use underscore if you work for underscore.io.