Skip to content

next on iterators is side-effecting, so use parens #837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions _ba/tour/mixin-class-composition.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ Razmotrimo apstrakciju za iteratore.
abstract class AbsIterator {
type T
def hasNext: Boolean
def next: T
def next(): T
}

Dalje, razmotrimo mixin klasu koja nasljeđuje `AbsIterator` s metodom `foreach` koja primjenjuje datu funkciju na svaki element iteratora.
Da bi definisali klasu koja može biti korištena kao mixin koristimo ključnu riječ `trait` (en. osobina, svojstvo).

trait RichIterator extends AbsIterator {
def foreach(f: T => Unit) { while (hasNext) f(next) }
def foreach(f: T => Unit) { while (hasNext) f(next()) }
}

Ovo je konkretna klasa iteratora, koja vraća sukcesivne karaktere datog stringa:
Expand All @@ -38,7 +38,7 @@ Ovo je konkretna klasa iteratora, koja vraća sukcesivne karaktere datog stringa
type T = Char
private var i = 0
def hasNext = i < s.length()
def next = { val ch = s charAt i; i += 1; ch }
def next() = { val ch = s charAt i; i += 1; ch }
}

Željeli bismo iskombinirati funkcionalnosti `StringIterator`-a i `RichIterator`-a u jednoj klasi.
Expand Down
6 changes: 3 additions & 3 deletions _es/tour/mixin-class-composition.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ A diferencia de lenguajes que solo soportan _herencia simple_, Scala tiene una n
abstract class AbsIterator {
type T
def hasNext: Boolean
def next: T
def next(): T
}

A continuación, considere una clase mezcla la cual extiende `AbsIterator` con un método `foreach` el cual aplica una función dada a cada elemento retornado por el iterador. Para definir una clase que puede usarse como una clase mezcla usamos la palabra clave `trait`.

trait RichIterator extends AbsIterator {
def foreach(f: T => Unit) { while (hasNext) f(next) }
def foreach(f: T => Unit) { while (hasNext) f(next()) }
}

Aquí se muestra una clase iterador concreta, la cual retorna caracteres sucesivos de una cadena de caracteres dada:
Expand All @@ -34,7 +34,7 @@ Aquí se muestra una clase iterador concreta, la cual retorna caracteres sucesiv
type T = Char
private var i = 0
def hasNext = i < s.length()
def next = { val ch = s charAt i; i += 1; ch }
def next() = { val ch = s charAt i; i += 1; ch }
}

Nos gustaría combinar la funcionalidad de `StringIterator` y `RichIterator` en una sola clase. Solo con herencia simple e interfaces esto es imposible, ya que ambas clases contienen implementaciones para sus miembros. Scala nos ayuda con sus _compisiciones de clases mezcladas_. Permite a los programadores reutilizar el delta de la definición de una clase, esto es, todas las nuevas definiciones que no son heredadas. Este mecanismo hace posible combinar `StringIterator` con `RichIterator`, como es hecho en el siguiente programa, el cual imprime una columna de todos los caracteres de una cadena de caracteres dada.
Expand Down
6 changes: 3 additions & 3 deletions _ko/tour/mixin-class-composition.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ _단일 상속_ 만을 지원하는 여러 언어와는 달리, 스칼라는 더
abstract class AbsIterator {
type T
def hasNext: Boolean
def next: T
def next(): T
}

이어서, 이터레이터가 반환하는 모든 항목에 주어진 함수를 적용해주는 `foreach` 메소드로 `AbsIterator`를 확장한 믹스인 클래스를 살펴보자. 믹스인으로 사용할 수 있는 클래스를 정의하기 위해선 `trait`이란 키워드를 사용한다.

trait RichIterator extends AbsIterator {
def foreach(f: T => Unit) { while (hasNext) f(next) }
def foreach(f: T => Unit) { while (hasNext) f(next()) }
}

다음은 주어진 문자열의 캐릭터를 차례로 반환해주는 콘크리트 이터레이터 클래스다.
Expand All @@ -33,7 +33,7 @@ _단일 상속_ 만을 지원하는 여러 언어와는 달리, 스칼라는 더
type T = Char
private var i = 0
def hasNext = i < s.length()
def next = { val ch = s charAt i; i += 1; ch }
def next() = { val ch = s charAt i; i += 1; ch }
}

`StringIterator`와 `RichIterator`를 하나의 클래스로 합치고 싶다면 어떻게 할까. 두 클래스 모두는 코드가 포함된 멤버 구현을 담고 있기 때문에 단일 상속과 인터페이스 만으론 불가능한 일이다. 스칼라는 _믹스인 클래스 컴포지션_ 으로 이런 상황을 해결해준다. 프로그래머는 이를 사용해 클래스 정의에서 변경된 부분을 재사용할 수 있다. 이 기법은 주어진 문자열에 포함된 모든 캐릭터를 한 줄로 출력해주는 다음의 테스트 프로그램에서와 같이, `StringIterator`와 `RichIterator`를 통합할 수 있도록 해준다.
Expand Down
6 changes: 3 additions & 3 deletions _pl/tour/mixin-class-composition.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ Rozważmy poniższe uogólnienie dla iteratorów:
abstract class AbsIterator {
type T
def hasNext: Boolean
def next: T
def next(): T
}
```

Następnie rozważmy klasę domieszkową, która doda do klasy `AbsIterator` metodę `foreach` wykonującą podaną funkcję dla każdego elementu zwracanego przez iterator. Aby zdefiniować klasę domieszkową, użyjemy słowa kluczowego `trait`:

```tut
trait RichIterator extends AbsIterator {
def foreach(f: T => Unit) { while (hasNext) f(next) }
def foreach(f: T => Unit) { while (hasNext) f(next()) }
}
```

Expand All @@ -39,7 +39,7 @@ class StringIterator(s: String) extends AbsIterator {
type T = Char
private var i = 0
def hasNext = i < s.length()
def next = { val ch = s charAt i; i += 1; ch }
def next() = { val ch = s charAt i; i += 1; ch }
}
```

Expand Down
8 changes: 4 additions & 4 deletions _pt-br/tour/mixin-class-composition.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ Ao contrário de linguagens que suportam somente _herança simples_, Scala tem u
abstract class AbsIterator {
type T
def hasNext: Boolean
def next: T
def next(): T
}
```

A seguir, considere a classe mixin que estende `AbsIterator` com um método `foreach` que aplica uma dada função para cada elemento retornado pelo iterator. Para definir tal classe que será utilizada como um mixin a palavra-chave `trait` deve ser declarada.

```tut
trait RichIterator extends AbsIterator {
def foreach(f: T => Unit) { while (hasNext) f(next) }
def foreach(f: T => Unit) { while (hasNext) f(next()) }
}
```

Expand All @@ -38,7 +38,7 @@ class StringIterator(s: String) extends AbsIterator {
type T = Char
private var i = 0
def hasNext = i < s.length()
def next = { val ch = s charAt i; i += 1; ch }
def next() = { val ch = s charAt i; i += 1; ch }
}
```

Expand All @@ -54,4 +54,4 @@ object StringIteratorTest {
}
```

A classe `Iter` na função `main` é construída a partir de uma composição dos pais `StringIterator` e `RichIterator` com a palavra-chave `with`. O primeiro pai é chamado de _superclass_ de `Iter`, já o segundo pai (e qualquer outro que venha após) é chamado de _mixin_ ou mescla.
A classe `Iter` na função `main` é construída a partir de uma composição dos pais `StringIterator` e `RichIterator` com a palavra-chave `with`. O primeiro pai é chamado de _superclass_ de `Iter`, já o segundo pai (e qualquer outro que venha após) é chamado de _mixin_ ou mescla.
6 changes: 3 additions & 3 deletions _tour/mixin-class-composition.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Now let's look at a more interesting example starting with an abstract class:
abstract class AbsIterator {
type T
def hasNext: Boolean
def next: T
def next(): T
}
```
The class has an abstract type `T` and the standard iterator methods.
Expand All @@ -51,7 +51,7 @@ class StringIterator(s: String) extends AbsIterator {
type T = Char
private var i = 0
def hasNext = i < s.length
def next = {
def next() = {
val ch = s charAt i
i += 1
ch
Expand All @@ -64,7 +64,7 @@ Now let's create a trait which also extends `AbsIterator`.

```tut
trait RichIterator extends AbsIterator {
def foreach(f: T => Unit): Unit = while (hasNext) f(next)
def foreach(f: T => Unit): Unit = while (hasNext) f(next())
}
```
Because `RichIterator` is a trait, it doesn't need to implement the abstract members of AbsIterator.
Expand Down