In Perl and Bash Shell-like environments the dollar sign is used to declare a variable.
I found some nice code snippet here in Forum, which helped me with some of my understandings considering Scala (very much appreciated! )
targets.foreach(t => println(s"$t: ${getCapitals(t)}"))
can someone please take apart this line, and explain how it works? I have taken it, that " $t " is just short for " $targets ", which we named apriori. Found out that when I do write it, it will just give " List ", and seems to jump out the foreach-loop.
Here is the whole code:
val countryCapitals = Map(
“Germany” → “Berlin”,
“Russia” → “Moscow”,
“Finland” → “Helsinki”)def getCapitals(country: String): String =
countryCapitals.get(country) match {case Some(capital) => capital
case None => “unknown”
}val targets = List(“Germany”, “Russia”, “New York”)
println( " Read capitals:" )
targets.foreach(t => println(s"$t: ${getCapitals(t)}"))