Gist: Scala Set

In Scala, an instance of Set can be declared as follows.

val s = (1 to 6).toSet

Principle difference between sets and sequences are three:

  • Sets are unordered
val s = (1 to 6).toSet
//> s : scala.collection.immutable.Set[Int] = Set(5, 1, 6, 2, 3, 4)
  • Sets do not have duplicate elements
s map (_ % 2 ==0)
//> res8: scala.collection.immutable.Set[Boolean] = Set(false, true)
  • The fundamental operation on set is contains.
s contains 5
//> res9: Boolean = true
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: