Skip to content

Commit 17557b3

Browse files
authored
Merge pull request #1654 from andreamarconi/patch-1
Coding example of "Variances" corrected
2 parents 24296ca + 079f0fd commit 17557b3

File tree

5 files changed

+18
-37
lines changed

5 files changed

+18
-37
lines changed

_pl/tour/multiple-parameter-lists.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,4 @@ numbers.foldRight(0)((sum, item) => sum + item) // postać ogólna
7373
7474
numbers.foldLeft(0)(_+_) // postać rozwijana (curried)
7575
numbers.foldRight(0)(_+_) // postać rozwijana (curried)
76-
77-
(0 /: numbers)(_+_) // Alternatywy sposób wywołania foldLeft
78-
(numbers :\ 0)(_+_) // Alternatywy sposób wywołania foldRight
7976
```

_ru/tour/multiple-parameter-lists.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,12 @@ numbers.foldRight(0)((sum, item) => sum + item) // Общая Форма
7878
7979
numbers.foldLeft(0)(_+_) // Форма с каррированием
8080
numbers.foldRight(0)(_+_) // Форма с каррированием
81-
82-
(0 /: numbers)(_+_) // Используется вместо foldLeft
83-
(numbers :\ 0)(_+_) // Используется вместо foldRight
84-
```
81+
```
8582

8683

8784
#### Неявные параметры
8885
Чтоб указать что параметр используется неявно (`implicit`) необходимо задавать несколько списков параметров. Примером может служить следующее:
8986

9087
```
9188
def execute(arg: Int)(implicit ec: ExecutionContext) = ???
92-
```
89+
```

_tour/variances.md

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,19 @@ Both `Cat` and `Dog` are subtypes of `Animal`. The Scala standard library has a
3737
In the following example, the method `printAnimalNames` will accept a list of animals as an argument and print their names each on a new line. If `List[A]` were not covariant, the last two method calls would not compile, which would severely limit the usefulness of the `printAnimalNames` method.
3838

3939
```tut
40-
object CovarianceTest extends App {
41-
def printAnimalNames(animals: List[Animal]): Unit = {
42-
animals.foreach { animal =>
43-
println(animal.name)
44-
}
40+
def printAnimalNames(animals: List[Animal]): Unit =
41+
animals.foreach {
42+
animal => println(animal.name)
4543
}
4644
47-
val cats: List[Cat] = List(Cat("Whiskers"), Cat("Tom"))
48-
val dogs: List[Dog] = List(Dog("Fido"), Dog("Rex"))
45+
val cats: List[Cat] = List(Cat("Whiskers"), Cat("Tom"))
46+
val dogs: List[Dog] = List(Dog("Fido"), Dog("Rex"))
4947
50-
printAnimalNames(cats)
51-
// Whiskers
52-
// Tom
48+
// prints: Whiskers, Tom
49+
printAnimalNames(cats)
5350
54-
printAnimalNames(dogs)
55-
// Fido
56-
// Rex
57-
}
51+
// prints: Fido, Rex
52+
printAnimalNames(dogs)
5853
```
5954

6055
### Contravariance
@@ -86,19 +81,14 @@ class CatPrinter extends Printer[Cat] {
8681
If a `Printer[Cat]` knows how to print any `Cat` to the console, and a `Printer[Animal]` knows how to print any `Animal` to the console, it makes sense that a `Printer[Animal]` would also know how to print any `Cat`. The inverse relationship does not apply, because a `Printer[Cat]` does not know how to print any `Animal` to the console. Therefore, we should be able to substitute a `Printer[Animal]` for a `Printer[Cat]`, if we wish, and making `Printer[A]` contravariant allows us to do exactly that.
8782

8883
```tut
89-
object ContravarianceTest extends App {
90-
val myCat: Cat = Cat("Boots")
91-
92-
def printMyCat(printer: Printer[Cat]): Unit = {
93-
printer.print(myCat)
94-
}
84+
def printMyCat(printer: Printer[Cat], cat: Cat): Unit =
85+
printer.print(cat)
9586
96-
val catPrinter: Printer[Cat] = new CatPrinter
97-
val animalPrinter: Printer[Animal] = new AnimalPrinter
87+
val catPrinter: Printer[Cat] = new CatPrinter
88+
val animalPrinter: Printer[Animal] = new AnimalPrinter
9889
99-
printMyCat(catPrinter)
100-
printMyCat(animalPrinter)
101-
}
90+
printMyCat(catPrinter, Cat("Boots"))
91+
printMyCat(animalPrinter, Cat("Boots"))
10292
```
10393

10494
The output of this program will be:

_zh-cn/tour/multiple-parameter-lists.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ numbers.foldRight(0)((sum, item) => sum + item) // Generic Form
6565
6666
numbers.foldLeft(0)(_+_) // Curried Form
6767
numbers.foldRight(0)(_+_) // Curried Form
68-
69-
(0 /: numbers)(_+_) // Used in place of foldLeft
70-
(numbers :\ 0)(_+_) // Used in place of foldRight
7168
```
7269

7370

scripts/run-tut.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ set -eux
55
# black-list _overviews/contributors/index.md because it embeds a Markdown snippet which embeds Scala code that does not compile
66
mv _overviews/contributors/index.md /tmp/_overviews_contributors_index.md
77

8-
coursier launch -r "https://dl.bintray.com/tpolecat/maven/" org.tpolecat:tut-core_2.12:0.6.7 -- . tut-tmp '.*\.md$' -classpath $(coursier fetch -p com.chuusai:shapeless_2.12:2.3.3) -Xfatal-warnings -feature
8+
coursier launch -r "https://dl.bintray.com/tpolecat/maven/" org.tpolecat:tut-core_2.12:0.6.13 -- . tut-tmp '.*\.md$' -classpath $(coursier fetch -p com.chuusai:shapeless_2.12:2.3.3) -Xfatal-warnings -feature
99

1010
# restore _overviews/contributors/index.md file
1111
mv /tmp/_overviews_contributors_index.md _overviews/contributors/index.md

0 commit comments

Comments
 (0)