From fb176e65eaeaa795c4357991feb9fd82197766a4 Mon Sep 17 00:00:00 2001 From: Seth Tisue Date: Tue, 1 Aug 2017 18:22:51 -0400 Subject: [PATCH] next on iterators is side-effecting, so use parens as per discussion at https://users.scala-lang.org/t/help-understanding-a-function-parameter-and-f-next/1480 --- _ba/tour/mixin-class-composition.md | 6 +++--- _es/tour/mixin-class-composition.md | 6 +++--- _ko/tour/mixin-class-composition.md | 6 +++--- _pl/tour/mixin-class-composition.md | 6 +++--- _pt-br/tour/mixin-class-composition.md | 8 ++++---- _tour/mixin-class-composition.md | 6 +++--- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/_ba/tour/mixin-class-composition.md b/_ba/tour/mixin-class-composition.md index 379d464211..3ae82edd13 100644 --- a/_ba/tour/mixin-class-composition.md +++ b/_ba/tour/mixin-class-composition.md @@ -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: @@ -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. diff --git a/_es/tour/mixin-class-composition.md b/_es/tour/mixin-class-composition.md index 5a48fc0a8b..5a0a8aa8e1 100644 --- a/_es/tour/mixin-class-composition.md +++ b/_es/tour/mixin-class-composition.md @@ -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: @@ -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. diff --git a/_ko/tour/mixin-class-composition.md b/_ko/tour/mixin-class-composition.md index c290082dca..128b225755 100644 --- a/_ko/tour/mixin-class-composition.md +++ b/_ko/tour/mixin-class-composition.md @@ -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()) } } 다음은 주어진 문자열의 캐릭터를 차례로 반환해주는 콘크리트 이터레이터 클래스다. @@ -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`를 통합할 수 있도록 해준다. diff --git a/_pl/tour/mixin-class-composition.md b/_pl/tour/mixin-class-composition.md index ed358ac8ca..33212e175b 100644 --- a/_pl/tour/mixin-class-composition.md +++ b/_pl/tour/mixin-class-composition.md @@ -20,7 +20,7 @@ Rozważmy poniższe uogólnienie dla iteratorów: abstract class AbsIterator { type T def hasNext: Boolean - def next: T + def next(): T } ``` @@ -28,7 +28,7 @@ Następnie rozważmy klasę domieszkową, która doda do klasy `AbsIterator` met ```tut trait RichIterator extends AbsIterator { - def foreach(f: T => Unit) { while (hasNext) f(next) } + def foreach(f: T => Unit) { while (hasNext) f(next()) } } ``` @@ -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 } } ``` diff --git a/_pt-br/tour/mixin-class-composition.md b/_pt-br/tour/mixin-class-composition.md index fea1eca336..1835b39e25 100644 --- a/_pt-br/tour/mixin-class-composition.md +++ b/_pt-br/tour/mixin-class-composition.md @@ -19,7 +19,7 @@ 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 } ``` @@ -27,7 +27,7 @@ A seguir, considere a classe mixin que estende `AbsIterator` com um método `for ```tut trait RichIterator extends AbsIterator { - def foreach(f: T => Unit) { while (hasNext) f(next) } + def foreach(f: T => Unit) { while (hasNext) f(next()) } } ``` @@ -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 } } ``` @@ -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. \ No newline at end of file +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. diff --git a/_tour/mixin-class-composition.md b/_tour/mixin-class-composition.md index ca9e33d69b..470f294826 100644 --- a/_tour/mixin-class-composition.md +++ b/_tour/mixin-class-composition.md @@ -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. @@ -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 @@ -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.