Skip to content

Instantly share code, notes, and snippets.

@dirkmc
Created December 22, 2011 08:34
Show Gist options
  • Save dirkmc/1509528 to your computer and use it in GitHub Desktop.
Save dirkmc/1509528 to your computer and use it in GitHub Desktop.
Scala List for each

Use foreach to iterate over a list

val fruits = List("banana", "orange", "strawberry", "apple", "pear")
fruits.foreach(fruit => println(fruit))

For simple operations you can just use an underscore instead of naming the argument:

val fruits = List("banana", "orange", "strawberry", "apple", "pear")
fruits.foreach(println(_))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment