Title: Loop A Collection
Slug: loop_a_collection
Summary: Loop A Collection Using Scala.
Date: 2017-01-03 12:00
Category: Scala
Tags: Basics
Authors: Chris Albon
If you want to learn more, check out Scala Cookbook and Programming in Scala.
In [12]:
val vector = Vector("Male", 2, true)
In [20]:
// For each item in the collection, print the class type of the element
vector.foreach((i: Any) => println(i, i.getClass.getSimpleName))
In [22]:
// For each item in the collection
vector.foreach {
// If one of these, print "Man"
case "Male" | "M" | "Man" | "Gentleman" | "Boy" => println("Man")
// For everything else, print "Something Else"
case _ => println("Something Else")
}