Movatterモバイル変換


[0]ホーム

URL:


Collections (Scala 2.8 - 2.12)

Introduction

    Language

      Martin Odersky, and Lex Spoon

      In the eyes of many, the new collections framework is the most significantchange in the Scala 2.8 release. Scala had collections before (and in fact the newframework is largely compatible with them). But it’s only 2.8 thatprovides a common, uniform, and all-encompassing framework forcollection types.

      Even though the additions to collections are subtle at first glance,the changes they can provoke in your programming style can beprofound. In fact, quite often it’s as if you work on a higher-levelwith the basic building blocks of a program being whole collectionsinstead of their elements. This new style of programming requires someadaptation. Fortunately, the adaptation is helped by several niceproperties of the new Scala collections. They are easy to use,concise, safe, fast, universal.

      Easy to use: A small vocabulary of 20-50 methods isenough to solve most collection problems in a couple of operations. Noneed to wrap your head around complicated looping structures orrecursions. Persistent collections and side-effect-free operations meanthat you need not worry about accidentally corrupting existingcollections with new data. Interference between iterators andcollection updates is eliminated.

      Concise: You can achieve with a single word what used totake one or several loops. You can express functional operations withlightweight syntax and combine operations effortlessly, so that the resultfeels like a custom algebra.

      Safe: This one has to be experienced to sink in. Thestatically typed and functional nature of Scala’s collections meansthat the overwhelming majority of errors you might make are caught atcompile-time. The reason is that (1) the collection operationsthemselves are heavily used and therefore welltested. (2) the usages of the collection operation make inputs andoutput explicit as function parameters and results. (3) These explicitinputs and outputs are subject to static type checking. The bottom lineis that the large majority of misuses will manifest themselves as typeerrors. It’s not at all uncommon to have programs of several hundredlines run at first try.

      Fast: Collection operations are tuned and optimized in thelibraries. As a result, using collections is typically quiteefficient. You might be able to do a little better with carefullyhand-tuned data structures and operations, but you might also do a lotworse by making some suboptimal implementation decisions along theway. What’s more, collections have been recently adapted to parallelexecution on multi-cores. Parallel collections support the sameoperations as sequential ones, so no new operations need to be learnedand no code needs to be rewritten. You can turn a sequential collection into aparallel one simply by invoking thepar method.

      Universal: Collections provide the same operations onany type where it makes sense to do so. So you can achieve a lot witha fairly small vocabulary of operations. For instance, a string isconceptually a sequence of characters. Consequently, in Scalacollections, strings support all sequence operations. The same holdsfor arrays.

      Example: Here’s one line of code that demonstrates many of theadvantages of Scala’s collections.

      val (minors, adults) = people partition (_.age < 18)

      It’s immediately clear what this operation does: It partitions acollection ofpeople intominors andadults depending ontheir age. Because thepartition method is defined in the rootcollection typeTraversableLike, this code works for any kind ofcollection, including arrays. The resultingminors andadultscollections will be of the same type as thepeople collection.

      This code is much more concise than the one to three loops required fortraditional collection processing (three loops for an array, becausethe intermediate results need to be buffered somewhere else). Onceyou have learned the basic collection vocabulary you will also findwriting this code is much easier and safer than writing explicitloops. Furthermore, thepartition operation is quite fast, and willget even faster on parallel collections on multi-cores. (Parallelcollections have been releasedas part of Scala 2.9.)

      This document provides an in depth discussion of the APIs of theScala collections classes from a user perspective. It takes you ona tour of all the fundamental classes and the methods they define.

      Contributors to this page:

      Contents


      [8]ページ先頭

      ©2009-2025 Movatter.jp