8000 Merge pull request #17 from outsideris/overview/collections/overview · nectarine/scala.github.com@ef77ac4 · GitHub
[go: up one dir, main page]

Skip to content

Commit ef77ac4

Browse files
committed
Merge pull request codeport#17 from outsideris/overview/collections/overview
closed codeport#2 collections/overview.md 번역
2 parents 0a400bb + be5164b commit ef77ac4

File tree

1 file changed

+72
-86
lines changed

1 file changed

+72
-86
lines changed

ko/overviews/collections/overview.md

Lines changed: 72 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
layout: overview-large
3-
title: 가변성 컬렉션과 불변성 컬렉션
3+
title: 변경할 수 있는 컬렉션과 변경할 수 없는 컬렉션
44

55
disqus: true
66

@@ -9,107 +9,94 @@ num: 2
99
language: ko
1010
---
1111

12-
스칼라 컬렉션에서는 불변성(immutable)인 컬렉션과 가변성(mutable)인 컬렉션을
13-
체계적으로 구분한다. _가변(mutable)_ 컬렉션은 변경하거나 확장이 가능하다. 즉,
14-
부작용을 통해 원소를 변경하거나 추가하거나 삭제할 수 있다. 반대로 _불변(immutable)_
15-
컬렉션은 결코 변화되지 않는다. 원소 삭제, 추가, 변경을 모방하는 연산이 있긴 하지만,
16-
그런 경우 새로운 컬렉션이 반환될 뿐 원래의 컬렉션은 변하지 않는다.
17-
18-
19-
All collection classes are found in the package `scala.collection` or
20-
one of its sub-packages `mutable`, `immutable`, and `generic`. Most
21-
collection classes needed by client code exist in three variants,
22-
which are located in packages `scala.collection`,
23-
`scala.collection.immutable`, and `scala.collection.mutable`,
24-
respectively. Each variant has different characteristics with respect
25-
to mutability.
26-
27-
A collection in package `scala.collection.immutable` is guaranteed to
28-
be immutable for everyone. Such a collection will never change after
29-
it is created. Therefore, you can rely on the fact that accessing the
30-
same collection value repeatedly at different points in time will
31-
always yield a collection with the same elements.
32-
33-
A collection in package `scala.collection.mutable` is known to have
34-
some operations that change the collection in place. So dealing with
35-
mutable collection means you need to understand which code changes
36-
which collection when.
37-
38-
A collection in package `scala.collection` can be either mutable or
39-
immutable. For instance, [collection.IndexedSeq\[T\]](http://www.scala-lang.org/api/current/scala/collection/IndexedSeq.html)
40-
is a superclass of both [collection.immutable.IndexedSeq\[T\]](http://www.scala-lang.org/api/current/scala/collection/immutable/IndexedSeq.html)
41-
and
42-
[collection.mutable.IndexedSeq\[T\]](http://www.scala-lang.org/api/current/scala/collection/mutable/IndexedSeq.html)
43-
Generally, the root collections in
44-
package `scala.collection` define the same interface as the immutable
45-
collections, and the mutable collections in package
46-
`scala.collection.mutable` typically add some side-effecting
47-
modification operations to this immutable interface.
48-
49-
The difference between root collections and immutable collections is
50-
that clients of an immutable collection have a guarantee that nobody
51-
can mutate the collection, whereas clients of a root collection only
52-
promise not to change the collection themselves. Even though the
53-
static type of such a collection provides no operations for modifying
54-
the collection, it might still be possible that the run-time type is a
55-
mutable collection which can be changed by other clients.
56-
57-
By default, Scala always picks immutable collections. For instance, if
58-
you just write `Set` without any prefix or without having imported
59-
`Set` from somewhere, you get an immutable set, and if you write
60-
`Iterable` you get an immutable iterable collection, because these
61-
are the default bindings imported from the `scala` package. To get
62-
the mutable default versions, you need to write explicitly
63-
`collection.mutable.Set`, or `collection.mutable.Iterable`.
12+
시스템적으로 스칼라의 컬렉션을 변경할 수 있는(mutable) 컬렉션과 변경할 수 없는(immutable)
13+
컬렉션으로 나눌 수 있다. _변경할 수 있는_ 컬렉션은 바로 수정하고 확장할 수 있다. 즉, 컬렉션
14+
요소를 부가작용으로 변경하고 추가하거나 제거할 수 있다. 반면에 _변경할 수 없는_ 컬렉션은
15+
절대 변경되지 않는다. _변경할 수 없는_ 컬렉션에서도 추가나 삭제, 갱신같은 작업을 할 수 있지만
16+
이는 기존의 컬렉션은 변경하지 않고 놔둔 채 새로운 컬렉션은 반환한다.
17+
18+
모든 컬렉션 클래스는 `scala.collection` 패키지와 `scala.collection` 패키지의 하위
19+
패키지인 `mutable`, `immutable`, `generic`에 있다. 클라이언트 코드에서 필요한
20+
컬렉션 클래스의 대부분은 `scala.collection`, `scala.collection.immutable`,
21+
`scala.collection.mutable` 패키지에 있다.
22+
23+
`scala.collection.immutable` 패키지의 컬렉션은 변경되지 않는다는 것을 보장하고 생성된
24+
이후에 절대로 변경되지 않을 것이다. 그래서 언제 어디서든 반복적으로 같은 컬렉션에 접근하더라도
25+
항상 같은 요소를 가진 컬렉션에 접근한다는 것을 믿을 수 있다.
26+
27+
`scala.collection.mutable` 패키지의 컬렉션은 컬렉션을 즉석에서 변경하는 작업을 가지고
28+
있다. 그러므로 변경할 수 있는 컬렉션을 다룰 때는 코드가 언제 컬렉션을 변경하는 지를
29+
이해해야 한다.
30+
31+
`scala.collection` 패키지의 컬렉션은 변경할 수 있을 수도 없을 수도 있다.
32+
예를 들어 [collection.IndexedSeq\[T\]](http://www.scala-lang.org/api/current/scala/collection/IndexedSeq.html)
33+
[collection.immutable.IndexedSeq\[T\]](http://www.scala-lang.org/api/current/scala/collection/immutable/IndexedSeq.html)
34+
[collection.mutable.IndexedSeq\[T\]](http://www.scala-lang.org/api/current/scala/collection/mutable/IndexedSeq.html) 모두의 슈퍼클래스이다. 일반적으로 `scala.collection` 패키지의 루트 컬렉션은
35+
변경할 수 없는 컬렉션과 같은 인터페이스를 정의하고 `scala.collection.mutable`
36+
패키지의 변경할 수 있는 컬렉션은 이 변경할 수 없는 인터페이스에 부가작용을 하는
37+
수정작업이 추가되어 있다.
38+
39+
루트 컬렉션과 변경할 수 없는 컬렉션의 차이점은 루트 컬렉션은 루트 컬렉션의 클라이언트만
40+
컬렉션을 변경할 수 없지만 변경할 수 없는 컬렉션은 아무도 컬렉션을 변경할 수 없다는 점이다.
41+
루트 컬렉션의 정적 타임이 컬렉션을 변경하는 작업을 제공하지 않더라도 런타임에서의 타입은
42+
다른 클라이언트가 변경할 수 있는 컬렉션일 수 있다.
43+
44+
스칼라는 기본적으로 항상 변경할 수 없는 컬렉션을 사용한다. 예를 들어 접두사를 사용하지 않고
45+
다른 곳에서 `Set`을 임포트하지 않고 `Set`을 사용하면 변경할 수 없는 Set이 되고
46+
`Iterable`를 작성하면 변경할 수 없는 iterable 컬렉션이 된다. 이는 `scala` 패키지로
47+
임포트한 기본 바인딩때문이다. 변경가능한 컬렉션을 사용하려면 명시적으로
48+
`collection.mutable.Set``collection.mutable.Iterable`를 사용해야 한다.
6449

6550
A useful convention if you want to use both mutable and immutable
6651
versions of collections is to import just the package
6752
`collection.mutable`.
53+
변경할 수 있는 컬렉션과 변경할 수 없는 컬렉션을 둘 다 사용할 때 유용한 관례는
54+
`collection.mutable` 패키지를 임포트하는 것이다.
6855

6956
import scala.collection.mutable
7057

7158
Then a word like `Set` without a prefix still refers to an an immutable collection,
72-
whereas `mutable.Set` refers to the mutable counterpart.
59+
whereas `mutable.Set` refers to the mutable counterpart.
60+
이렇게 임포트한 뒤 접두사가 없는 `Set`은 변경할 수 없는 컬렉션을 계속 참조하고
7361

74-
The last package in the collection hierarchy is `collection.generic`. This
75-
package contains building blocks for implementing
76-
collections. Typically, collection classes defer the implementations
77-
of some of their operations to classes in `generic`. Users of the
78-
collection framework on the other hand should need to refer to
79-
classes in `generic` only in exceptional circumstances.
62+
그 다음 접두사 없이 사용한 `Set`은 불변 컬렉션을 참조하고 `mutable.Set`
63+
가변 컬렉션을 참조한다.
8064

81-
For convenience and backwards compatibility some important types have
82-
aliases in the `scala` package, so you can use them by their simple
83 F438 -
names without needing an import. An example is the `List` type, which
84-
can be accessed alternatively as
65+
컬렉션 계층에서 남은 패키지는 `collection.generic`이다. 이 패키지에는 컬렉션을 구현하는
66+
코드가 있다. 일반적으로 컬렉션 클래스는 클래스의 일부 작업의 구현을 `generic`의 클래스로
67+
위임한다. 그럼에도 컬렉션 프레임워크의 사용자들은 예외적인 상황에서만 `generic`의 클래스를
68+
참조해야 한다.
8569

86-
scala.collection.immutable.List // that's where it is defined
70+
편의성과 하위 호환성때문에 일부 중요한 타입은 `scala` 패키지에 별칭을 가지고 있으므로
71+
임포트하지 않고도 간단한 이름으로 이러한 타입을 사용할 수 있다. 이 대표적인 예가 `List`
72+
타입이고 다음과 같은 방법으로 접근할 수 있다.
73+
74+
scala.collection.immutable.List // List가 정의되거 있는 곳
8775
scala.List // via the alias in the scala package
88-
List // because scala._
89-
// is always automatically imported
76+
scala.List // scala 패키지의 별칭으로 접근
77+
List // 항상 scala._가 자동 임포트되므로
9078

91-
Other types so aliased are
92-
[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).
79+
별칭이 있는 다른 타입으로는
80+
[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), [Range](http://www.scala-lang.org/api/current/scala/collection/immutable/Range.html)가 있다.
9381

94-
The following figure shows all collections in package
95-
`scala.collection`. These are all high-level abstract classes or traits, which
96-
generally have mutable as well as immutable implementations.
82+
다음 그림에 `scala.collection` 패키지의 모든 컬렉션이 나와 있다. 일반적으로 변경할 수 없는
83+
구현체 뿐 아니라 변경할 수 있는 구현체도 가지는 고수준의 추상 클래스나 트레이트가 있다.
9784

9885
[<img src="{{ site.baseurl }}/resources/images/collections.png" width="550">]({{ site.baseurl }}/resources/images/collections.png)
9986

100-
The following figure shows all collections in package `scala.collection.immutable`.
87+
다음 그림에는 `scala.collection.immutable` 패키지의 모든 컬렉션이 나와 있다.
10188

10289
[<img src="{{ site.baseurl }}/resources/images/collections.immutable.png" width="550">]({{ site.baseurl }}/resources/images/collections.immutable.png)
10390

104-
And the following figure shows all collections in package `scala.collection.mutable`.
91+
다음 그림에는 `scala.collection.mutable` 패키지의 모든 컬렉션이 나와 있다.
10592

10693
[<img src="{{ site.baseurl }}/resources/images/collections.mutable.png" width="550">]({{ site.baseurl }}/resources/images/collections.mutable.png)
94+
(이 세 그림은 decodified.com의 Matthias로 만들었다.)
10795

108-
(All three figures were generated by Matthias at decodified.com).
109-
110-
## An Overview of the Collections API ##
96+
## 컬렉션 API 살펴보기 ##
11197

112-
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:
98+
위의 크림에 가장 중요한 컬렉션 클래스가 나와 있고 모든 클래스에 공통된 아주 약간의 특성이 있는데
99+
예를 들면 모든 컬렉션을 컬렉션 클래스명뒤에 요소를 작성하는 형태의 일관된 문법으로 생성할 수 있다.
113100

114101
Traversable(1, 2, 3)
115102
Iterable("x", "y", "z")
@@ -120,23 +107,22 @@ The most important collection classes are shown in the figures above. There is q
120107
IndexedSeq(1.0, 2.0)
121108
LinearSeq(a, b, c)
122109

123-
The same principle also applies for specific collection implementations, such as:
110+
같은 원리가 특정 컬렉션 구현체에도 다음과 같이 적용된다.
124111

125112
List(1, 2, 3)
126113
HashMap("x" -> 24, "y" -> 25, "z" -> 26)
127114

128-
All these collections get displayed with `toString` in the same way they are written above.
115+
위에 작성한 것과 같은 방법으로 이러한 모든 컬렉션을 `toString`으로 표현한다.
129116

130-
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.
117+
모든 컬렉션은 `Traversable`이 제공하는 API를 지원하지만 가능하다면 타입을 전문화한다. 예를 들어 `Traversable`클래스의 `map`메서드를 실행하면 새로운 `Traversable`을 반환하지만 이 결과 타입은 하위 클래스에서 오버라이드된다. 즉, `List`에서 `map`을 호출하면 다시 `List`를 반환하고 `Set`에서 `map`을 호출하면 다시 `Set`을 반환하는 등이다.
131118

132-
scala> List(1, 2, 3) map (_ + 1)
119+
scala> List(1, 2, 3) map (_ + 1)
133120
res0: List[Int] = List(2, 3, 4)
134121
scala> Set(1, 2, 3) map (_ * 2)
135122
res0: Set[Int] = Set(2, 4, 6)
136123

137-
This behavior which is implemented everywhere in the collections libraries is called the _uniform return type principle_.
138-
139-
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.
124+
컬렉션 라이브러리 어디서나 구현된 이 동작을 _통일된 반환 타입 원리(uniform return type principle)_라고 부른다.
140125

141-
In the following, we will review these classes one by one.
126+
컬렉션 계층의 클래스 대부분은 3가지 종류가 있는데 루트 컬렉션과 변경할 수 있는 컬렉션과 변경할 수 없는 컬렉션이다. Buffer 트레이트만이 변경할 수 있는 컬렉션만 있는 유일한 예외이다.
142127

128+
이어서 이러한 클래스를 하나씩 살펴 볼 것이다.

0 commit comments

Comments
 (0)
0