Skip to content

Commit fdabbee

Browse files
committed
Refactoring to automate index pages.
1 parent fbda34d commit fdabbee

9 files changed

Lines changed: 261 additions & 7 deletions

File tree

‎_includes/index-header.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,21 @@
9494

9595
.bottom ul {
9696
margin: 0 0 18px 25px;
97+
list-style: square;
9798
}
9899

99100
.bottom ul li {
100101
color: #404040;
101102
font-size: 18px;
102103
line-height: 24px;
104+
margin: 0px;
103105
}
104106

105107
.bottom ul li p {
106108
color: #404040;
107109
font-size: 18px;
108110
line-height: 24px;
111+
margin: 0px;
109112
}
110113

111114
.bottom ul li a {

‎_layouts/index.html‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ <h2>{{ page.title }}</h2>
1717
{{ content }}
1818
</div>
1919
<div class="span6">
20-
Right sidebar.
2120
</div>
2221

2322
</div>

‎overviews/collections/01-intro.md‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
---
2+
layout: overview
3+
title: Introduction
4+
disqus: true
5+
label-color: success
6+
label-text: Available in Part
7+
category: collections
8+
---
9+
10+
**Martin Odersky, and Lex Spoon**
11+
12+
In the eyes of many, the new collections framework is the most significant
13+
change in the Scala 2.8 release. Scala had collections before (and in fact the new
14+
framework is largely compatible with them). But it's only 2.8 that
15+
provides a common, uniform, and all-encompassing framework for
16+
collection types.
17+
18+
Even though the additions to collections are subtle at first glance,
19+
the changes they can provoke in your programming style can be
20+
profound. In fact, quite often it's as if you work on a higher-level
21+
with the basic building blocks of a program being whole collections
22+
instead of their elements. This new style of programming requires some
23+
adaptation. Fortunately, the adaptation is helped by several nice
24+
properties of the new Scala collections. They are easy to use,
25+
concise, safe, fast, universal.
26+
27+
**Easy to use:** A small vocabulary of 20-50 methods is
28+
enough to solve most collection problems in a couple of operations. No
29+
need to wrap your head around complicated looping structures or
30+
recursions. Persistent collections and side-effect-free operations mean
31+
that you need not worry about accidentally corrupting existing
32+
collections with new data. Interference between iterators and
33+
collection updates is eliminated.
34+
35+
**Concise:** You can achieve with a single word what used to
36+
take one or several loops. You can express functional operations with
37+
lightweight syntax and combine operations effortlessly, so that the result
38+
feels like a custom algebra.
39+
40+
**Safe:** This one has to be experienced to sink in. The
41+
statically typed and functional nature of Scala's collections means
42+
that the overwhelming majority of errors you might make are caught at
43+
compile-time. The reason is that (1) the collection operations
44+
themselves are heavily used and therefore well
45+
tested. (2) the usages of the collection operation make inputs and
46+
output explicit as function parameters and results. (3) These explicit
47+
inputs and outputs are subject to static type checking. The bottom line
48+
is that the large majority of misuses will manifest themselves as type
49+
errors. It's not at all uncommon to have programs of several hundred
50+
lines run at first try.
51+
52+
**Fast:** Collection operations are tuned and optimized in the
53+
libraries. As a result, using collections is typically quite
54+
efficient. You might be able to do a little bit better with carefully
55+
hand-tuned data structures and operations, but you might also do a lot
56+
worse by making some suboptimal implementation decisions along the
57+
way. What's more, collections have been recently adapted to parallel
58+
execution on multi-cores. Parallel collections support the same
59+
operations as sequential ones, so no new operations need to be learned
60+
and no code needs to be rewritten. You can turn a sequential collection into a
61+
parallel one simply by invoking the `par` method.
62+
63+
**Universal:** Collections provide the same operations on
64+
any type where it makes sense to do so. So you can achieve a lot with
65+
a fairly small vocabulary of operations. For instance, a string is
66+
conceptually a sequence of characters. Consequently, in Scala
67+
collections, strings support all sequence operations. The same holds
68+
for arrays.
69+
70+
**Example:** Here's one line of code that demonstrates many of the
71+
advantages of Scala's collections.
72+
73+
val (minors, adults) = people partition (_.age < 18)
74+
75+
It's immediately clear what this operation does: It partitions a
76+
collection of `people` into `minors` and `adults` depending on
77+
their age. Because the `partition` method is defined in the root
78+
collection type `TraversableLike`, this code works for any kind of
79+
collection, including arrays. The resulting `minors` and `adults`
80+
collections will be of the same type as the `people` collection.
81+
82+
This code is much more concise than the one to three loops required for
83+
traditional collection processing (three loops for an array, because
84+
the intermediate results need to be buffered somewhere else). Once
85+
you have learned the basic collection vocabulary you will also find
86+
writing this code is much easier and safer than writing explicit
87+
loops. Furthermore, the `partition` operation is quite fast, and will
88+
get even faster on parallel collections on multi-cores. (Parallel
89+
collections have been released
90+
as part of Scala 2.9.)
91+
92+
This document provides an in depth discussion of the APIs of the
93+
Scala collections classes from a user perspective. They take you on
94+
a tour of all the fundamental classes and the methods they define.
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
---
2+
layout: overview
3+
title: Mutable and Immutable Collections
4+
disqus: true
5+
label-color: success
6+
label-text: Available in Part
7+
category: collections
8+
---
9+
10+
Scala collections systematically distinguish between mutable and
11+
immutable collections. A _mutable_ collection can be updated or
12+
extended in place. This means you can change, add, or remove elements
13+
of a collection as a side effect. _Immutable_ collections, by
14+
contrast, never change. You have still operations that simulate
15+
additions, removals, or updates, but those operations will in each
16+
case return a new collection and leave the old collection unchanged.
17+
18+
All collection classes are found in the package `scala.collection` or
19+
one of its sub-packages `mutable`, `immutable`, and `generic`. Most
20+
collection classes needed by client code exist in three variants,
21+
which are located in packages `scala.collection`,
22+
`scala.collection.immutable`, and `scala.collection.mutable`,
23+
respectively. Each variant has different characteristics with respect
24+
to mutability.
25+
26+
A collection in package `scala.collection.immutable` is guaranteed to
27+
be immutable for everyone. Such a collection will never change after
28+
it is created. Therefore, you can rely on the fact that accessing the
29+
same collection value repeatedly at different points in time will
30+
always yield a collection with the same elements.
31+
32+
A collection in package `scala.collection.mutable` is known to have
33+
some operations that change the collection in place. So dealing with
34+
mutable collection means you need to understand which code changes
35+
which collection when.
36+
37+
A collection in package `scala.collection` can be either mutable or
38+
immutable. For instance, [collection.IndexedSeq\[T\]](http://www.scala-lang.org/api/current/scala/collection/IndexedSeq.html)
39+
is a superclass of both [collection.immutable.IndexedSeq\[T\]](http://www.scala-lang.org/api/current/scala/collection/immutable/IndexedSeq.html)
40+
and
41+
[collection.mutable.IndexedSeq\[T\]](http://www.scala-lang.org/api/current/scala/collection/mutable/IndexedSeq.html)
42+
Generally, the root collections in
43+
package `scala.collection` define the same interface as the immutable
44+
collections, and the mutable collections in package
45+
`scala.collection.mutable` typically add some side-effecting
46+
modification operations to this immutable interface.
47+
48+
The difference between root collections and immutable collections is
49+
that clients of an immutable collection have a guarantee that nobody
50+
can mutate the collection, whereas clients of a root collection only
51+
promise not to change the collection themselves. Even though the
52+
static type of such a collection provides no operations for modifying
53+
the collection, it might still be possible that the run-time type is a
54+
mutable collection which can be changed by other clients.
55+
56+
By default, Scala always picks immutable collections. For instance, if
57+
you just write `Set` without any prefix or without having imported
58+
`Set` from somewhere, you get an immutable set, and if you write
59+
`Iterable` you get an immutable iterable collection, because these
60+
are the default bindings imported from the `scala` package. To get
61+
the mutable default versions, you need to write explicitly
62+
`collection.mutable.Set`, or `collection.mutable.Iterable`.
63+
64+
A useful convention if you want to use both mutable and immutable
65+
versions of collections is to import just the package
66+
`collection.mutable`.
67+
68+
import scala.collection.mutable
69+
70+
Then a word like `Set` without a prefix still refers to an an immutable collection,
71+
whereas `mutable.Set` refers to the mutable counterpart.
72+
73+
The last package in the collection hierarchy is `collection.generic`. This
74+
package contains building blocks for implementing
75+
collections. Typically, collection classes defer the implementations
76+
of some of their operations to classes in `generic`. Users of the
77+
collection framework on the other hand should need to refer to
78+
classes in `generic` only in exceptional circumstances.
79+
80+
For convenience and backwards compatibility some important types have
81+
aliases in the `scala` package, so you can use them by their simple
82+
names without needing an import. An example is the `List` type, which
83+
can be accessed alternatively as
84+
85+
scala.collection.immutable.List // that's where it is defined
86+
scala.List // via the alias in the scala package
87+
List // because scala._
88+
// is always automatically imported
89+
90+
Other types so aliased are
91+
[Traversable](http://www.scala-lang.org/api/current/scala/collection/Traversable.html), [Iterable](http://www.scala-lang.org/api/current/scala/collection/Iterable.html), [Seq](http://www.scala-lang.org/api/current/scala/collection/Seq.html), [IndexedSeq](http://www.scala-lang.org/api/current/scala/collection/IndexedSeq.html), [Iterator](http://www.scala-lang.org/api/current/scala/collection/Iterator.html), [Stream](http://www.scala-lang.org/api/current/scala/collection/immutable/Stream.html), [Vector](http://www.scala-lang.org/api/current/scala/collection/immutable/Vector.html), [StringBuilder](http://www.scala-lang.org/api/current/scala/collection/mutable/StringBuilder.html), and [Range](http://www.scala-lang.org/api/current/scala/collection/immutable/Range.html).
92+
93+
The following figure shows all collections in package
94+
`scala.collection`. These are all high-level abstract classes or traits, which
95+
generally have mutable as well as immutable implementations.
96+
97+
[<img src="{{ site.baseurl }}/resources/images/collections.png" width="550">]({{ site.baseurl }}/resources/images/collections.png)
98+
99+
The following figure shows all collections in package `scala.collection.immutable`.
100+
101+
[<img src="{{ site.baseurl }}/resources/images/collections.immutable.png" width="550">]({{ site.baseurl }}/resources/images/collections.immutable.png)
102+
103+
And the following figure shows all collections in package `scala.collection.mutable`.
104+
105+
[<img src="{{ site.baseurl }}/resources/images/collections.mutable.png" width="550">]({{ site.baseurl }}/resources/images/collections.mutable.png)
106+
107+
(All three figures were generated by Matthias at decodified.com).
108+
109+
## An Overview of the Collections API ##
110+
111+
The most important collection classes are shown in the figures above. There is quite a bit of commonality shared by all these classes. For instance, every kind of collection can be created by the same uniform syntax, writing the collection class name followed by its elements:
112+
113+
Traversable(1, 2, 3)
114+
Iterable("x", "y", "z")
115+
Map("x" -> 24, "y" -> 25, "z" -> 26)
116+
Set(Color.red, Color.green, Color.blue)
117+
SortedSet("hello", "world")
118+
Buffer(x, y, z)
119+
IndexedSeq(1.0, 2.0)
120+
LinearSeq(a, b, c)
121+
122+
The same principle also applies for specific collection implementations, such as:
123+
124+
List(1, 2, 3)
125+
HashMap("x" -> 24, "y" -> 25, "z" -> 26)
126+
127+
All these collections get displayed with `toString` in the same way they are written above.
128+
129+
All collections support the API provided by `Traversable`, but specialize types wherever this makes sense. For instance the `map` method in class `Traversable` returns another `Traversable` as its result. But this result type is overridden in subclasses. For instance, calling `map` on a `List` yields again a `List`, calling it on a `Set` yields again a `Set` and so on.
130+
131+
scala> List(1, 2, 3) map (_ + 1)
132+
res0: List[Int] = List(2, 3, 4)
133+
scala> Set(1, 2, 3) map (_ * 2)
134+
res0: Set[Int] = Set(2, 4, 6)
135+
136+
This behavior which is implemented everywhere in the collections libraries is called the _uniform return type principle_.
137+
138+
Most of the classes in the collections hierarchy exist in three variants: root, mutable, and immutable. The only exception is the Buffer trait which only exists as a mutable collection.
139+
140+
In the following, we will review these classes one by one.

overviews/collections.md renamed to overviews/core/_posts/2010-09-07-collections.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
layout: overview
33
title: Scala's Collections Library
44
disqus: true
5+
label-color: success
6+
label-text: Available in Part
7+
parent-dir: collections
58
---
69

710
**Martin Odersky, and Lex Spoon**

overviews/actors.md renamed to overviews/core/_posts/2010-11-30-actors.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
---
22
layout: overview
33
title: The Scala Actors API
4+
label-color: success
5+
label-text: Available
46
---
57

68
**Philipp Haller and Stephen Tu**

overviews/architecture-of-scala-collections.md renamed to overviews/core/_posts/2010-12-15-architecture-of-scala-collections.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
---
22
layout: overview
33
title: The Architecture of Scala Collections
4+
label-color: success
5+
label-text: Available
46
---
57

68
**Martin Odersky and Lex Spoon**

‎overviews/index.md‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ title: Guides and Overviews
77
<h1>Core <small>The essentials...</small></h1>
88
</div>
99

10-
* [Collections]({{ site.baseurl }}/overviews/collections) <span class="label success">Available in Part</span>
11-
* [Architecture of Scala's Collections Library]({{ site.baseurl }}/overviews/architecture-of-scala-collections.html) <span class="label success">Available</span>
12-
* [Actors]({{ site.baseurl }}/overviews/actors.html) <span class="label success">Available</span>
10+
{% for post in site.categories.core %}
11+
* [{{ post.title }}]({{ site.baseurl }}{{ post.url }}) {% if post.label %}<span class="label {{ post.label-color }}">{{ post.label-text }}</span>{% endif %}
12+
{% if post.parent-dir %}
13+
<ul>
14+
{% for page in site.categories.core %}
15+
<li>+1</li>
16+
{% endfor %}
17+
</ul>
18+
{% endif %}
19+
{% endfor %}
1320
* Swing <span class="label important">In Progress</span>
1421

15-
16-
17-

‎sips.new/sip-list.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ layout: sip-landing
33
title: List of SIPs
44
---
55

6+
### Pending SIPs ###
7+
<ul class="post-list">
8+
{% for post in site.categories.pending %}
9+
<li><a href="{{ site.baseurl }}{{ post.url }}">{{ post.title }}</a> <span class="date">( {{ post.date | date: "%b %Y" }} )</span></li>
10+
{% endfor %}
11+
</ul>
12+
613
### Completed SIPs ###
714
<ul class="post-list">
815
{% for post in site.categories.completed %}

0 commit comments

Comments
 (0)