Skip to content
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

#850:Tuples page added to tour #1144

Merged
merged 1 commit into from
Sep 21, 2018
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
2 changes: 1 addition & 1 deletion _ba/tour/mixin-class-composition.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ partof: scala-tour

num: 6
next-page: higher-order-functions
previous-page: traits
previous-page: tuples
prerequisite-knowledge: inheritance, traits, abstract-classes, unified-types

---
Expand Down
2 changes: 1 addition & 1 deletion _ba/tour/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ discourse: true
partof: scala-tour

num: 5
next-page: mixin-class-composition
next-page: tuples
previous-page: classes
assumed-knowledge: expressions, classes, generics, objects, companion-objects

Expand Down
16 changes: 16 additions & 0 deletions _ba/tour/tuples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
layout: tour
title: Tuples
language: ba

discourse: true

partof: scala-tour
num:
next-page: mixin-class-composition
previous-page: traits

---

(this section of the tour has not been translated yet. pull request
with translation welcome!)
2 changes: 1 addition & 1 deletion _es/tour/inner-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ partof: scala-tour
num: 11
language: es

next-page: mixin-class-composition
next-page: tuples
previous-page: implicit-parameters
---

Expand Down
2 changes: 1 addition & 1 deletion _es/tour/mixin-class-composition.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ num: 12
language: es

next-page: singleton-objects
previous-page: inner-classes
previous-page: tuples
---
_Nota de traducción: La palabra `mixin` puede ser traducida como mezcla, dando título a esta sección de: Composición de clases Mezcla, pero es preferible utilizar la notación original_

Expand Down
17 changes: 17 additions & 0 deletions _es/tour/tuples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
layout: tour
title: Tuples

discourse: false

partof: scala-tour
num:
language: es

next-page: mixin-class-composition
previous-page: inner-classes

---

(this section of the tour has not been translated yet. pull request
with translation welcome!)
2 changes: 1 addition & 1 deletion _ko/tour/mixin-class-composition.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ num: 6
language: ko

next-page: higher-order-functions
previous-page: traits
previous-page: tuples
---

_단일 상속_ 만을 지원하는 여러 언어와는 달리, 스칼라는 더욱 일반화된 시각에서 클래스를 재사용한다. 스칼라는 새로운 클래스를 정의할 때 _클래스의 새로운 멤버 정의_ (즉, 슈퍼클래스와 비교할 때 변경된 부분)를 재사용할 수 있다. 이를 _믹스인 클래스 컴포지션_ 이라고 부른다. 이터레이터를 추상화한 다음 예제를 살펴보자.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be replaced by the stub to indicate it hasn't been translated yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is some translation there, not sure though if it corresponds to English version.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mistake, I got confused by the fact that this page is actually about mixins, and it's just the "previous" pointer that's being updated.

Expand Down
2 changes: 1 addition & 1 deletion _ko/tour/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ partof: scala-tour
num: 5
language: ko

next-page: mixin-class-composition
next-page: tuples
previous-page: classes
---

Expand Down
17 changes: 17 additions & 0 deletions _ko/tour/tuples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
layout: tour
title: Tuples

discourse: true

partof: scala-tour
num:
language: ko

next-page: mixin-class-composition
previous-page: traits

---

(this section of the tour has not been translated yet. pull request
with translation welcome!)
2 changes: 1 addition & 1 deletion _pl/tour/mixin-class-composition.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ partof: scala-tour
num: 5
language: pl
next-page: higher-order-functions
previous-page: traits
previous-page: tuples
---

W przeciwieństwie do języków, które wspierają jedynie pojedyncze dziedziczenie, Scala posiada bardziej uogólniony mechanizm ponownego wykorzystania klas. Scala umożliwia wykorzystanie _nowych elementów klasy_ (różnicy w stosunku do klasy bazowej) w definicji nowej klasy. Wyraża się to przy pomocy _kompozycji domieszek_.
Expand Down
2 changes: 1 addition & 1 deletion _pl/tour/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ partof: scala-tour

num: 4
language: pl
next-page: mixin-class-composition
next-page: tuples
previous-page: classes
---

Expand Down
16 changes: 16 additions & 0 deletions _pl/tour/tuples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
layout: tour
title: Tuples

discourse: true

partof: scala-tour
num:
language: pl
next-page: mixin-class-composition
previous-page: traits

---

(this section of the tour has not been translated yet. pull request
with translation welcome!)
2 changes: 1 addition & 1 deletion _pt-br/tour/mixin-class-composition.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ partof: scala-tour

num: 5
next-page: higher-order-functions
previous-page: traits
previous-page: tuples
language: pt-br
---
_Nota de tradução: A palavra `mixin` pode ser traduzida como mescla, porém é preferível utilizar a notação original_
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be replaced by the stub to indicate it hasn't been translated yet.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an explanation, but it does not correspond to the English version of the page.

Copy link
Contributor

@adriaanm adriaanm Sep 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry, I got confused by the fact that this page is actually about mixins, and it's just the "previous" pointer that's being updated.

Expand Down
2 changes: 1 addition & 1 deletion _pt-br/tour/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ discourse: false
partof: scala-tour

num: 4
next-page: mixin-class-composition
next-page: tuples
previous-page: classes
language: pt-br
---
Expand Down
15 changes: 15 additions & 0 deletions _pt-br/tour/tuples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
layout: tour
title: Tuples

discourse: true

partof: scala-tour
num:
next-page: mixin-class-composition
previous-page: traits
language: pt-br
---

(this section of the tour has not been translated yet. pull request
with translation welcome!)
2 changes: 1 addition & 1 deletion _th/tour/mixin-class-composition.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ num: 6
language: th

next-page: higher-order-functions
previous-page: traits
previous-page: tuples
---
2 changes: 1 addition & 1 deletion _th/tour/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ num: 5

language: th

next-page: mixin-class-composition
next-page: tuples
previous-page: classes
---
18 changes: 18 additions & 0 deletions _th/tour/tuples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
layout: tour
title: Tuples

discourse: true

partof: scala-tour

num:

language: th

next-page: mixin-class-composition
previous-page: traits
---

(this section of the tour has not been translated yet. pull request
with translation welcome!)
2 changes: 1 addition & 1 deletion _tour/mixin-class-composition.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ partof: scala-tour

num: 6
next-page: higher-order-functions
previous-page: traits
previous-page: tuples
prerequisite-knowledge: inheritance, traits, abstract-classes, unified-types

redirect_from: "/tutorials/tour/mixin-class-composition.html"
Expand Down
2 changes: 1 addition & 1 deletion _tour/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ discourse: true
partof: scala-tour

num: 5
next-page: mixin-class-composition
next-page: tuples
previous-page: classes
topics: traits
prerequisite-knowledge: expressions, classes, generics, objects, companion-objects
Expand Down
96 changes: 96 additions & 0 deletions _tour/tuples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
---
layout: tour
title: Tuples

discourse: true

partof: scala-tour

num:
next-page: mixin-class-composition
previous-page: traits
topics: tuples

redirect_from: "/tutorials/tour/tuples.html"
---

In Scala, a tuple is a class that can hold elements of different types.
Jasper-M marked this conversation as resolved.
Show resolved Hide resolved
Tuples are immutable.

Tuples come in handy when we have to return multiple values from a function.

A tuple can be created as:

```tut
val ingredient = ("Sugar" , 25):Tuple2[String, Int]
```
This creates a tuple containing a String element and an Int element.

Tuple in Scala is a series of classes: Tuple2, Tuple3, etc., through Tuple22.
So when we create a tuple with n elements(n lying between 2 and 22), Scala basically instantiates
one of the corresponding classes from the group, parameterized with types of constituent elements.
For eg., ingredient is of type Tuple2[String, Int].

## Accessing the elements

Tuple elements are accessed using underscore syntax.
'tuple._n' gives nth element(given there are that many elements).

```tut
println(ingredient._1) // Sugar

println(ingredient._2) // 25
```

## Destructuring tuple data

Scala tuple also supports destructuring.

```tut
val (name, quantity) = ingredient

println(name) // Sugar

println(quantity) // 25
```

Tuple destructuring can be used in pattern matching too.

```tut
val planetDistanceFromSun = List(("Mercury", 57.9), ("Venus", 108.2), ("Earth", 149.6 ), ("Mars", 227.9), ("Jupiter", 778.3))

planetDistanceFromSun.foreach{ tuple => {

tuple match {

case ("Mercury", distance) => println(s"Mercury is $distance millions km far from Sun")

case p if(p._1 == "Venus") => println(s"Venus is ${p._2} millions km far from Sun")

case p if(p._1 == "Earth") => println(s"Blue planet is ${p._2} millions km far from Sun")

case _ => println("Too far....")

}

}

}
```

Or, in 'for' comprehension.

```tut
val numPairs = List((2, 5), (3, -7), (20, 56))

for ((a, b) <- numPairs) {

println(a * b)

}
```

The value () of type Unit is conceptually the same as the value () of type Tuple0. There can only be one value of this type since it has no elements.

Users may sometimes find hard to chose between Tuples and case classes. As a rule, case classes are preferred choice if elements
carry more meaning.
2 changes: 1 addition & 1 deletion _zh-cn/tour/mixin-class-composition.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ num: 6
language: zh-cn

next-page: higher-order-functions
previous-page: traits
previous-page: tuples
---
2 changes: 1 addition & 1 deletion _zh-cn/tour/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ num: 5

language: zh-cn

next-page: mixin-class-composition
next-page: tuples
previous-page: classes
topics: traits
prerequisite-knowledge: expressions, classes, generics, objects, companion-objects
Expand Down
19 changes: 19 additions & 0 deletions _zh-cn/tour/tuples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
layout: tour
title: Tuples

discourse: true

partof: scala-tour

num:

language: zh-cn

next-page: mixin-class-composition
previous-page: traits
topics: tuples
---

(this section of the tour has not been translated yet. pull request
with translation welcome!)