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

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

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

Добавлен: 02.01.2026

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

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

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

 

List of Listings

xxx

19.1

A basic functional queue. . . . . . . . . . . . . . . . . . .

425

19.2

Hiding a primary constructor by making it private. . . . . .

426

19.3

An apply factory method in a companion object. . . . . . .

427

19.4

Type abstraction for functional queues. . . . . . . . . . . .

428

19.5

A nonvariant (rigid) Cell class. . . . . . . . . . . . . . . .

431

19.6

A type parameter with a lower bound. . . . . . . . . . . . .

437

19.7

A contravariant output channel. . . . . . . . . . . . . . . .

438

19.8

Covariance and contravariance of Function1s. . . . . . . .

439

19.9

Demonstration of function type parameter variance. . . . .

440

19.10

An optimized functional queue. . . . . . . . . . . . . . . .

442

19.11

A Person class that mixes in the Ordered trait. . . . . . .

444

19.12

A merge sort function with an upper bound. . . . . . . . .

444

20.1

Overriding abstract vals and parameterless methods. . . .

450

20.2

Declaring abstract vars. . . . . . . . . . . . . . . . . . . .

450

20.3

How abstract vars are expanded into getters and setters. . .

451

20.4

A trait that uses its abstract vals. . . . . . . . . . . . . . .

452

20.5

Pre-initialized fields in an anonymous class expression. . .

454

20.6

Pre-initialized fields in an object definition. . . . . . . . . .

454

20.7

Pre-initialized fields in a class definition. . . . . . . . . . .

455

20.8

Initializing a trait with lazy vals. . . . . . . . . . . . . . .

456

20.9

Modeling suitable food with an abstract type. . . . . . . . .

460

20.10

Implementing an abstract type in a subclass. . . . . . . . .

461

20.11

The US currency zone. . . . . . . . . . . . . . . . . . . . .

473

20.12

Currency zones for Europe and Japan. . . . . . . . . . . .

475

20.13

A converter object with an exchange rates map. . . . . . .

476

20.14

The full code of class CurrencyZone. . . . . . . . . . . .

477

21.1

An implicit parameter list with multiple parameters. . . . .

491

21.2

A function with an upper bound. . . . . . . . . . . . . . .

493

21.3

A function with an implicit parameter. . . . . . . . . . . .

494

21.4

A function that uses an implicit parameter internally. . . . .

496

21.5

A function with a view bound. . . . . . . . . . . . . . . . .

497

21.6

Sample code that uses an implicit parameter. . . . . . . . .

500

21.7

Sample code after type checking and insertion of implicits.

500

22.1

The definition of the Nil singleton object. . . . . . . . . .

505

22.2

Prepending a supertype element to a subtype list. . . . . . .

507

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


 

List of Listings

xxxi

22.3

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

507

22.4

The definition of method ::: in class List. . . . . . . . .

509

22.5

The definition of method map in class List. . . . . . . . .

511

22.6

The definition of the :: subclass of List. . . . . . . . . .

512

24.1

Mixing in the SynchronizedMap trait. . . . . . . . . . . .

563

25.1

An outline of the Builder class. . . . . . . . . . . . . . .

608

25.2

Implementation of filter in TraversableLike. . . . . .

609

25.3

Implementation of map in TraversableLike. . . . . . . .

612

25.4

The CanBuildFrom trait. . . . . . . . . . . . . . . . . . .

612

25.5

RNA Bases. . . . . . . . . . . . . . . . . . . . . . . . . .

614

25.6

RNA strands class, first version. . . . . . . . . . . . . . . .

615

25.7

RNA strands class, second version. . . . . . . . . . . . . .

618

25.8

RNA strands class, final version. . . . . . . . . . . . . . .

622

25.9

RNA companion object—final version. . . . . . . . . . . .

623

25.10

An implementation of prefix maps with Patricia tries. . . .

626

25.11

The companion object for prefix maps. . . . . . . . . . . .

629

26.1

The EMail string extractor object. . . . . . . . . . . . . . .

633

26.2

The Twice string extractor object. . . . . . . . . . . . . . .

636

26.3

The UpperCase string extractor object. . . . . . . . . . . .

636

26.4

The Domain string extractor object. . . . . . . . . . . . . .

638

26.5

The ExpandedEMail extractor object. . . . . . . . . . . . .

639

26.6

An extractor that defines an unapplySeq method. . . . . .

640

26.7

How the r method is defined in StringOps. . . . . . . . .

644

29.1

A simple Food entity class. . . . . . . . . . . . . . . . . .

671

29.2

Simple Recipe entity class. . . . . . . . . . . . . . . . . .

672

29.3

Food and Recipe examples for use in tests. . . . . . . . . .

672

29.4

Mock database and browser modules. . . . . . . . . . . . .

673

29.5

Database and browser modules with categories added. . . .

674

29.6

A Browser class with an abstract database val. . . . . . .

675

29.7

A Database class with abstract methods. . . . . . . . . . .

676

29.8

The SimpleDatabase object as a Database subclass. . . .

676

29.9

The SimpleBrowser object as a Browser subclass. . . . .

677

29.10

A student database and browser. . . . . . . . . . . . . . . .

677

29.11

A trait for food categories. . . . . . . . . . . . . . . . . . .

678

29.12

A Database class that mixes in the FoodCategories trait.

678

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


 

List of Listings

xxxii

29.13

A SimpleDatabase object composed solely of mixins. . .

678

29.14

A SimpleFoods trait. . . . . . . . . . . . . . . . . . . . .

678

29.15

A SimpleRecipes trait with a self type. . . . . . . . . . .

679

29.16

An app that dynamically selects a module implementation.

680

29.17

Using a singleton type. . . . . . . . . . . . . . . . . . . .

682

30.1

A superclass equals method that calls canEqual. . . . . .

696

30.2

A subclass equals method that calls canEqual. . . . . . .

697

30.3

Hierarchy for binary trees. . . . . . . . . . . . . . . . . . .

699

30.4

A parameterized type with equals and hashCode. . . . . .

703

30.5

Class Rational with equals and hashCode. . . . . . . . .

704

31.1

A Scala method that declares a Java throws clause. . . . .

715

32.1

A simple actor. . . . . . . . . . . . . . . . . . . . . . . . .

726

32.2

An actor that calls receive. . . . . . . . . . . . . . . . . .

728

32.3

An actor that calls react. . . . . . . . . . . . . . . . . . .

732

32.4

An actor’s act method that uses loop. . . . . . . . . . . .

733

32.5

An actor that uses a helper actor to avoid blocking itself. . .

735

32.6

An actor that uses case classes for messages. . . . . . . . .

740

32.7

The Simulant trait. . . . . . . . . . . . . . . . . . . . . .

748

32.8

Adder components. . . . . . . . . . . . . . . . . . . . . .

755

33.1

An arithmetic expression parser. . . . . . . . . . . . . . . .

761

33.2

A regular expression parser for Java identifiers. . . . . . . .

763

33.3

Data in JSON format. . . . . . . . . . . . . . . . . . . . .

765

33.4

A simple JSON parser. . . . . . . . . . . . . . . . . . . . .

766

33.5

A full JSON parser that returns meaningful results. . . . . .

770

33.6

The ~ combinator method. . . . . . . . . . . . . . . . . . .

778

34.1

A simple Swing application in Scala. . . . . . . . . . . . .

789

34.2

Component assembly on a panel. . . . . . . . . . . . . . .

791

34.3

Implementing a reactive Swing application. . . . . . . . . .

795

34.4

An implementation of the temperature converter. . . . . . .

797

35.1

Code for spreadsheet in Figure 35.1. . . . . . . . . . . . .

802

35.2

The main program for the spreadsheet application. . . . . .

803

35.3

A spreadsheet with a rendererComponent method. . . . .

804

35.4

First version of the Model class. . . . . . . . . . . . . . . .

805

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


 

List of Listings

xxxiii

35.5

Classes representing formulas. . . . . . . . . . . . . . . .

807

35.6

A spreadsheet that parses formulas. . . . . . . . . . . . . .

811

35.7

The evaluate method of trait Evaluator. . . . . . . . . .

814

35.8

A library for arithmetic operations. . . . . . . . . . . . . .

816

35.9

The finished spreadsheet component. . . . . . . . . . . . .

822

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


Foreword

I’m not sure where I first came across the Scala language. Maybe on a forum for programming language enthusiasts such as Lambda the Ultimate, or maybe in more pedestrian quarters: Reddit, or the like. Although I was intrigued at first blush, I owe my deeper exploration and enthusiasm for the language to two individuals: David Pollak, creator of the Lift web framework, and Steve Jenson, a former colleague at Twitter and generally brilliant programmer.

Following David and Steve, I arrived to Scala in the late-middle stage of the language’s history to date. By 2008, Scala had spent five years evolving from its initial release, and had formed around it a tight-knit community of academics, tinkerers, and even a few consultants. The mailing lists were full of spirited debates, announcements of exciting libraries, and a general camaraderie and shared joy for seeing what this powerful new tool could do. What Scala lacked, at that point, was a collection of success stories around major production deployments.

The decision to use Scala at Twitter, where I then worked, was not an easy one to make. Our infrastructure was buckling under the weight of extreme growth. Picking a relative unknown as our language of choice for building the high-performance distributed systems that would keep our fledgling service alive was risky. Still, the benefits that Scala offered were (and are) compelling, and our engineers were quickly able to produce prototypes that proved out the language’s effectiveness.

In the intervening time, I’ve seen a heartening number of companies large and small adopting Scala. In that time, too, the question of Scala’s complexity has been raised. From the outside, Scala’s many features might appear to be a kind of complexity. To understand Scala, though, is to understand its goal of being a scalable language. You can be writing real-world code in Scala in an afternoon. As your understanding of the language and, indeed,

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

Foreword

xxxv

of the art and science of programming as a whole expands, there’s more of Scala there to wield to your advantage. That’s not complexity. It’s flexibility.

To be clear: Scala will challenge you. That’s part of the joy of using it. You won’t understand the full power of its type system by the end of your first day. You won’t understand the zen of objects being functions and functions being objects in your first week. Each feature of the language is another light bulb waiting to switch on over your head. I’m certain you’ll enjoy the experience of being gradually illuminated as you read this book and write code. I’ve watched programmers learn Scala on the job and succeed. It can be done, and it can be fun.

As Scala programmers like me have grown to better understand what this powerful language can do, so too has Scala evolved to meet programmers’ needs. Scala 2.8 smoothes out some rough spots in the collection libraries and adds useful features like named and default arguments to methods. While Scala has been a perfectly productive language to work with for some time, as of 2.8 it feels even more solid and polished. The new 2.8 release is icing on the cake.

In my experience, Scala was ready for production deployments two years ago. Today, it’s even better, and I can’t imagine building a new system without it. Presently, I’m doing just that. For me, Scala has gone from being a risky gamble to a trusted tool in two short years. I look forward to taking advantage of the latest features in Scala 2.8, and to using this book as the definitive reference for it, direct from the creator of the language I’ve grown to depend on.

Alex Payne

Portland, Oregon

October 27, 2010

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