Introduction to Scala Part2 : Collections

Bubu
6,331 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Traversable: size info

  • Operations: isEmpty, nonEmpty, size, and hasDefiniteSize
  • Goal: Get some information on the size of the collection

Traversable collections can be finite or infinite. An example of an infinite traversable collection is the stream of natural numbers Stream.from(0).

The method hasDefiniteSize indicates whether a collection is possibly infinite. If hasDefiniteSize returns true, the collection is certainly finite. If it returns false, the collection has not been not fully elaborated yet, so it might be infinite or finite. (Stream will be see, if we have enough time in Course C#03)

> List(1,2,3).size
res0: Int = 3
Determine If It's an Empty List or Not
Determine If a List Is Big
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content