ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 02.01.2026

Просмотров: 3411

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Section 22.1

Chapter 22 · Implementing Lists

506

The definitions of the head and tail method simply return the corresponding parameter. In fact, this pattern can be abbreviated by letting the parameters directly implement the head and tail methods of the superclass List, as in the following equivalent but shorter definition of the :: class:

final case class ::[T](head: T, tail: List[T]) extends List[T] {

override def isEmpty: Boolean = false

}

This works because every case class parameter is implicitly also a field of the class (it’s like the parameter declaration was prefixed with val). Recall from Section 20.3 that Scala allows you to implement an abstract parameterless method such as head or tail with a field. So the code above directly uses the parameters head and tail as implementations of the abstract methods head and tail that were inherited from class List.

Some more methods

All other List methods can be written using the basic three. For instance:

def length: Int =

if (isEmpty) 0 else 1 + tail.length

or:

def drop(n: Int): List[T] = if (isEmpty) Nil

else if (n <= 0) this else tail.drop(n - 1)

or:

def map[U](f: T => U): List[U] = if (isEmpty) Nil

else f(head) :: tail.map(f)

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index

Section 22.1

Chapter 22 · Implementing Lists

507

List construction

The list construction methods :: and ::: are special. Because they end in a colon, they are bound to their right operand. That is, an operation such as x :: xs is treated as the method call xs.::(x), not x.::(xs). In fact, x.::(xs) would not make sense, as x is of the list element type, which can be arbitrary, so we cannot assume that this type would have a :: method.

For this reason, the :: method should take an element value and yield a new list. What is the required type of the element value? You might be tempted to say, it should be the same as the list’s element type, but in fact this is more restrictive than necessary. To see why, consider this class hierarchy:

abstract class Fruit class Apple extends Fruit

class Orange extends Fruit

Listing 22.2 shows what happens when you construct lists of fruit:

scala> val apples = new Apple :: Nil apples: List[Apple] = List(Apple@585fa9)

scala> val fruits = new Orange :: apples

fruits: List[Fruit] = List(Orange@cd6798, Apple@585fa9)

Listing 22.2 · Prepending a supertype element to a subtype list.

The apples value is treated as a List of Apples, as expected. However, the definition of fruits shows that it’s still possible to add an element of a different type to that list. The element type of the resulting list is Fruit, which is the most precise common supertype of the original list element type (i.e., Apple) and the type of the element to be added (i.e., Orange). This flexibility is obtained by defining the :: method (cons) as shown in Listing 22.3:

def ::[U >: T](x: U): List[U] = new scala.::(x, this)

Listing 22.3 · The definition of method :: (cons) in class List.

Note that the method is itself polymorphic—it takes a type parameter named U. Furthermore, U is constrained in [U >: T] to be a supertype of the

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index


Section 22.1

Chapter 22 · Implementing Lists

508

 

 

 

 

 

Orange

Apple

head

head

 

::

 

::

tail

tail

 

fruits

apples

Nil

Figure 22.2 · The structure of the Scala lists shown in Listing 22.2.

list element type T. The element to be added is required to be of type U and the result is a List[U].

With the formulation of :: shown in Listing 22.3, you can check how the definition of fruits shown in Listing 22.2 works out type-wise: in that definition the type parameter U of :: is instantiated to Fruit. The lower-bound constraint of U is satisfied, because the list apples has type List[Apple] and Fruit is a supertype of Apple. The argument to the :: is new Orange, which conforms to type Fruit. Therefore, the method application is typecorrect with result type List[Fruit]. Figure 22.2 illustrates the structure of the lists that result from executing the code shown in Listing 22.3.

In fact, the polymorphic definition of :: with the lower bound T is not only convenient; it is also necessary to render the definition of class List type-correct. This is because Lists are defined to be covariant. Assume for a moment that we had defined :: like this:

// A thought experiment (which wouldn’t work) def ::(x: T): List[T] = new scala.::(x, this)

You saw in Chapter 19 that method parameters count as contravariant positions, so the list element type T is in contravariant position in the definition above. But then List cannot be declared covariant in T. The lower bound [U >: T] thus kills two birds with one stone: it removes a typing problem, and it leads to a :: method that’s more flexible to use.

The list concatenation method ::: is defined in a similar way to ::, as shown in Listing 22.4.

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index



Section 22.2

Chapter 22 · Implementing Lists

509

def :::[U >: T](prefix: List[U]): List[U] = if (prefix.isEmpty) this

else prefix.head :: prefix.tail ::: this

Listing 22.4 · The definition of method ::: in class List.

Like cons, concatenation is polymorphic. The result type is “widened” as necessary to include the types of all list elements. Note also that again the order of the arguments is swapped between an infix operation and an explicit method call. Because both ::: and :: end in a colon, they both bind to the right and are both right associative. For instance, the else part of the definition of ::: shown in Listing 22.4 contains infix operations of both :: and :::. These infix operations can be expanded to equivalent method calls as follows:

prefix.head :: prefix.tail ::: this

equals (because :: and ::: are right-associative)

prefix.head :: (prefix.tail ::: this) equals (because :: binds to the right)

(prefix.tail ::: this).::(prefix.head) equals (because ::: binds to the right)

this.:::(prefix.tail).::(prefix.head)

22.2 The ListBuffer class

The typical access pattern for a list is recursive. For instance, to increment every element of a list without using map you could write:

def incAll(xs: List[Int]): List[Int] = xs match { case List() => List()

case x :: xs1 => x + 1 :: incAll(xs1)

}

One shortcoming of this program pattern is that it is not tail recursive. Note that the recursive call to incAll above occurs inside a :: operation. Therefore each recursive call requires a new stack frame. On today’s virtual ma-

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index


Section 22.2

Chapter 22 · Implementing Lists

510

chines this means that you cannot apply incAll to lists of much more than about 30,000 to 50,000 elements. This is a pity.

How do you write a version of incAll that can work on lists of arbitrary size (as much as heap-capacity allows)? One approach is to use a loop:

for (x <- xs) // ??

But what should go in the loop body? Note that where incAll above constructs the list by prepending elements to the result of the recursive call, the loop needs to append new elements at the end of the result list. One, very inefficient possibility is to use :::, the list append operator:

var result = List[Int]()

// a very inefficient approach

for (x <- xs) result = result ::: List(x + 1) result

This has terrible efficiency, though. Because ::: takes time proportional to the length of its first operand, the whole operation takes time proportional to the square of the length of the list. This is clearly unacceptable.

A better alternative is to use a list buffer. List buffers let you accumulate the elements of a list. To do this, you use an operation such as “buf += elem”, which appends the element elem at the end of the list buffer buf. Once you are done appending elements, you can turn the buffer into a list using the toList operation.

ListBuffer is a class in package scala.collection.mutable. To use the simple name only, you can import ListBuffer from its package:

import scala.collection.mutable.ListBuffer

Using a list buffer, the body of incAll can now be written as follows:

val buf = new ListBuffer[Int] for (x <- xs) buf += x + 1 buf.toList

This is a very efficient way to build lists. In fact, the list buffer implementation is organized so that both the append operation (+=) and the toList operation take (very short) constant time.

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index

Section 22.3

Chapter 22 · Implementing Lists

511

22.3 The List class in practice

The implementations of list methods given in Section 22.1 are concise and clear, but suffer from the same stack overflow problem as the non-tail recursive implementation of incAll. Therefore, most methods in the real implementation of class List avoid recursion and use loops with list buffers instead. For example, Listing 22.5 shows the real implementation of map in class List:

final override def map[U](f: T => U): List[U] = { val b = new ListBuffer[U]

var these = this

while (!these.isEmpty) { b += f(these.head) these = these.tail

}

b.toList

}

Listing 22.5 · The definition of method map in class List.

This revised implementation traverses the list with a simple loop, which is highly efficient. A tail recursive implementation would be similarly efficient, but a general recursive implementation would be slower and less scalable. But what about the operation b.toList at the end? What is its complexity? In fact, the call to the toList method takes only a small number of cycles, which is independent of the length of the list.

To understand why, take a second look at class ::, which constructs nonempty lists. In practice, this class does not quite correspond to its idealized definition given previously in Section 22.1. The real definition is shown in Listing 22.6.

There’s one peculiarity: the tl argument is a var! This means that it is possible to modify the tail of a list after the list is constructed. However, because the variable tl has the modifier private[scala], it can be accessed only from within package scala. Client code outside this package can neither read nor write tl.

Since the ListBuffer class is contained in a subpackage of package scala, scala.collection.mutable, ListBuffer can access the tl field

Cover · Overview · Contents · Discuss · Suggest · Glossary · Index