1

I'm trying to understand the use of map function and that underscore _ in the code below. keys is a List[String] and df is a DateFrame. I run an sample and found out listOfVal is a list of column type, but could someone help to explain how this works? What does _ mean in this case and what gets applied by map fuction? Many thanks

val listOfVal = keys.map(df(_))

ps: I've read the two questions suggested but I think they are different use cases

user4046073
  • 821
  • 4
  • 18
  • 39
  • 2
    Possible duplicate of [Underscores in a Scala map/foreach](https://stackoverflow.com/questions/47006863/underscores-in-a-scala-map-foreach) and https://stackoverflow.com/questions/37598649/why-can-i-not-flatmap-a-listoption-using-underscore – Ramesh Maharjan Jun 12 '18 at 04:17

1 Answers1

3

In Scala, _ can act as a place-holder for an anonymous function. For example:

List("A", "B", "C").map(_.toLowerCase)
// `_.toLowerCase` represents anonymous function `x => x.toLowerCase`
// res1: List[String] = List(a, b, c)

List(1, 2, 3, 4, 5).foreach(print(_))
// `print(_)` represents anonymous function `x => print(x)`
// res2: 12345

In your sample code, keys.map(df(_)) is equivalent to:

keys.map(c => df(c))

Let's say your keys is a list of column names:

List[String]("col1", "col2", "col3")

Then it simply gets mapped to:

List[Column](df("col1"), df("col2"), df("col3"))
Leo C
  • 22,006
  • 3
  • 26
  • 39