-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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!) |
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!) |
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!) |
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!) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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_ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
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!) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,5 +11,5 @@ num: 6 | |
language: th | ||
|
||
next-page: higher-order-functions | ||
previous-page: traits | ||
previous-page: tuples | ||
--- |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,6 @@ num: 5 | |
|
||
language: th | ||
|
||
next-page: mixin-class-composition | ||
next-page: tuples | ||
previous-page: classes | ||
--- |
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!) |
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,5 +11,5 @@ num: 6 | |
language: zh-cn | ||
|
||
next-page: higher-order-functions | ||
previous-page: traits | ||
previous-page: tuples | ||
--- |
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!) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.