From a597225ea4057e78ec5552073b9f140a26a5812a Mon Sep 17 00:00:00 2001 From: ghildiya Date: Wed, 12 Sep 2018 23:13:57 +0530 Subject: [PATCH] #850:Tuples page added to tour --- _ba/tour/mixin-class-composition.md | 2 +- _ba/tour/traits.md | 2 +- _ba/tour/tuples.md | 16 +++++ _es/tour/inner-classes.md | 2 +- _es/tour/mixin-class-composition.md | 2 +- _es/tour/tuples.md | 17 +++++ _ko/tour/mixin-class-composition.md | 2 +- _ko/tour/traits.md | 2 +- _ko/tour/tuples.md | 17 +++++ _pl/tour/mixin-class-composition.md | 2 +- _pl/tour/traits.md | 2 +- _pl/tour/tuples.md | 16 +++++ _pt-br/tour/mixin-class-composition.md | 2 +- _pt-br/tour/traits.md | 2 +- _pt-br/tour/tuples.md | 15 ++++ _th/tour/mixin-class-composition.md | 2 +- _th/tour/traits.md | 2 +- _th/tour/tuples.md | 18 +++++ _tour/mixin-class-composition.md | 2 +- _tour/traits.md | 2 +- _tour/tuples.md | 96 ++++++++++++++++++++++++++ _zh-cn/tour/mixin-class-composition.md | 2 +- _zh-cn/tour/traits.md | 2 +- _zh-cn/tour/tuples.md | 19 +++++ 24 files changed, 230 insertions(+), 16 deletions(-) create mode 100644 _ba/tour/tuples.md create mode 100644 _es/tour/tuples.md create mode 100644 _ko/tour/tuples.md create mode 100644 _pl/tour/tuples.md create mode 100644 _pt-br/tour/tuples.md create mode 100644 _th/tour/tuples.md create mode 100644 _tour/tuples.md create mode 100644 _zh-cn/tour/tuples.md diff --git a/_ba/tour/mixin-class-composition.md b/_ba/tour/mixin-class-composition.md index 7a1940bcc0..cc25efbc16 100644 --- a/_ba/tour/mixin-class-composition.md +++ b/_ba/tour/mixin-class-composition.md @@ -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 --- diff --git a/_ba/tour/traits.md b/_ba/tour/traits.md index d4ac8596e0..e9992f2203 100644 --- a/_ba/tour/traits.md +++ b/_ba/tour/traits.md @@ -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 diff --git a/_ba/tour/tuples.md b/_ba/tour/tuples.md new file mode 100644 index 0000000000..44873532a1 --- /dev/null +++ b/_ba/tour/tuples.md @@ -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!) \ No newline at end of file diff --git a/_es/tour/inner-classes.md b/_es/tour/inner-classes.md index c072021dc8..ee044d6fb9 100644 --- a/_es/tour/inner-classes.md +++ b/_es/tour/inner-classes.md @@ -9,7 +9,7 @@ partof: scala-tour num: 11 language: es -next-page: mixin-class-composition +next-page: tuples previous-page: implicit-parameters --- diff --git a/_es/tour/mixin-class-composition.md b/_es/tour/mixin-class-composition.md index 6343e0a7c6..18ab6b4f2f 100644 --- a/_es/tour/mixin-class-composition.md +++ b/_es/tour/mixin-class-composition.md @@ -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_ diff --git a/_es/tour/tuples.md b/_es/tour/tuples.md new file mode 100644 index 0000000000..dde02b6ded --- /dev/null +++ b/_es/tour/tuples.md @@ -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!) \ No newline at end of file diff --git a/_ko/tour/mixin-class-composition.md b/_ko/tour/mixin-class-composition.md index 262bdf5ab0..e4d2cac40f 100644 --- a/_ko/tour/mixin-class-composition.md +++ b/_ko/tour/mixin-class-composition.md @@ -10,7 +10,7 @@ num: 6 language: ko next-page: higher-order-functions -previous-page: traits +previous-page: tuples --- _단일 상속_ 만을 지원하는 여러 언어와는 달리, 스칼라는 더욱 일반화된 시각에서 클래스를 재사용한다. 스칼라는 새로운 클래스를 정의할 때 _클래스의 새로운 멤버 정의_ (즉, 슈퍼클래스와 비교할 때 변경된 부분)를 재사용할 수 있다. 이를 _믹스인 클래스 컴포지션_ 이라고 부른다. 이터레이터를 추상화한 다음 예제를 살펴보자. diff --git a/_ko/tour/traits.md b/_ko/tour/traits.md index a090581140..1d03cd971d 100644 --- a/_ko/tour/traits.md +++ b/_ko/tour/traits.md @@ -9,7 +9,7 @@ partof: scala-tour num: 5 language: ko -next-page: mixin-class-composition +next-page: tuples previous-page: classes --- diff --git a/_ko/tour/tuples.md b/_ko/tour/tuples.md new file mode 100644 index 0000000000..4463425bee --- /dev/null +++ b/_ko/tour/tuples.md @@ -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!) \ No newline at end of file diff --git a/_pl/tour/mixin-class-composition.md b/_pl/tour/mixin-class-composition.md index 5a2ec415ea..6e411a989e 100644 --- a/_pl/tour/mixin-class-composition.md +++ b/_pl/tour/mixin-class-composition.md @@ -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_. diff --git a/_pl/tour/traits.md b/_pl/tour/traits.md index 9d11ab5213..5d56863fff 100644 --- a/_pl/tour/traits.md +++ b/_pl/tour/traits.md @@ -8,7 +8,7 @@ partof: scala-tour num: 4 language: pl -next-page: mixin-class-composition +next-page: tuples previous-page: classes --- diff --git a/_pl/tour/tuples.md b/_pl/tour/tuples.md new file mode 100644 index 0000000000..d6516a89e6 --- /dev/null +++ b/_pl/tour/tuples.md @@ -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!) \ No newline at end of file diff --git a/_pt-br/tour/mixin-class-composition.md b/_pt-br/tour/mixin-class-composition.md index 4d000b3896..1d5fb71013 100644 --- a/_pt-br/tour/mixin-class-composition.md +++ b/_pt-br/tour/mixin-class-composition.md @@ -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_ diff --git a/_pt-br/tour/traits.md b/_pt-br/tour/traits.md index a7a750961f..fe36e46c5c 100644 --- a/_pt-br/tour/traits.md +++ b/_pt-br/tour/traits.md @@ -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 --- diff --git a/_pt-br/tour/tuples.md b/_pt-br/tour/tuples.md new file mode 100644 index 0000000000..8b81442317 --- /dev/null +++ b/_pt-br/tour/tuples.md @@ -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!) \ No newline at end of file diff --git a/_th/tour/mixin-class-composition.md b/_th/tour/mixin-class-composition.md index de57c2e0d7..833b8c8f83 100644 --- a/_th/tour/mixin-class-composition.md +++ b/_th/tour/mixin-class-composition.md @@ -11,5 +11,5 @@ num: 6 language: th next-page: higher-order-functions -previous-page: traits +previous-page: tuples --- diff --git a/_th/tour/traits.md b/_th/tour/traits.md index 87f1277df6..e91bcb983b 100644 --- a/_th/tour/traits.md +++ b/_th/tour/traits.md @@ -10,6 +10,6 @@ num: 5 language: th -next-page: mixin-class-composition +next-page: tuples previous-page: classes --- diff --git a/_th/tour/tuples.md b/_th/tour/tuples.md new file mode 100644 index 0000000000..3422f106e4 --- /dev/null +++ b/_th/tour/tuples.md @@ -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!) \ No newline at end of file diff --git a/_tour/mixin-class-composition.md b/_tour/mixin-class-composition.md index 67a32ee27a..18a0bcbb38 100644 --- a/_tour/mixin-class-composition.md +++ b/_tour/mixin-class-composition.md @@ -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" diff --git a/_tour/traits.md b/_tour/traits.md index bc01ac1f2d..121255457d 100644 --- a/_tour/traits.md +++ b/_tour/traits.md @@ -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 diff --git a/_tour/tuples.md b/_tour/tuples.md new file mode 100644 index 0000000000..7db0aac4dd --- /dev/null +++ b/_tour/tuples.md @@ -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. +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. \ No newline at end of file diff --git a/_zh-cn/tour/mixin-class-composition.md b/_zh-cn/tour/mixin-class-composition.md index 99e4119c92..b51db108c8 100644 --- a/_zh-cn/tour/mixin-class-composition.md +++ b/_zh-cn/tour/mixin-class-composition.md @@ -11,5 +11,5 @@ num: 6 language: zh-cn next-page: higher-order-functions -previous-page: traits +previous-page: tuples --- diff --git a/_zh-cn/tour/traits.md b/_zh-cn/tour/traits.md index 7c646b44f5..188a40def2 100644 --- a/_zh-cn/tour/traits.md +++ b/_zh-cn/tour/traits.md @@ -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 diff --git a/_zh-cn/tour/tuples.md b/_zh-cn/tour/tuples.md new file mode 100644 index 0000000000..76c284208a --- /dev/null +++ b/_zh-cn/tour/tuples.md @@ -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!) \ No newline at end of file