Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


Share this Page URL
Help

Chapter 3: Next Steps in Scala > Step 10. Use sets and maps - Pg. 47

S TEP 10 · Use sets and maps Accessing the elements of a tuple You may be wondering why you can't access the elements of a tuple like the elements of a list, for example, with " pair(0) ". The reason is that a list's apply method always returns the same type, but each element of a tuple may be a different type: _1 can have one result type, _2 another, and so on. These _N numbers are one-based, instead of zero-based, because starting with 1 is a tradition set by other languages with statically typed tuples, such as Haskell and ML. Step 10. Use sets and maps Because Scala aims to help you take advantage of both functional and im- perative styles, its collections libraries make a point to differentiate between mutable and immutable collections. For example, arrays are always muta- ble; lists are always immutable. Scala also provides mutable and immutable alternatives for sets and maps, but uses the same simple names for both ver- sions. For sets and maps, Scala models mutability in the class hierarchy.