From 7f63b65bd604939f7fcd9e629188dbf980446997 Mon Sep 17 00:00:00 2001 From: Horace Velmont Date: Tue, 2 Jul 2019 23:44:18 +0900 Subject: [PATCH 1/2] Add Korean translation for tuples --- _ko/tour/tuples.md | 70 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 6 deletions(-) diff --git a/_ko/tour/tuples.md b/_ko/tour/tuples.md index 4463425bee..85afa4d01d 100644 --- a/_ko/tour/tuples.md +++ b/_ko/tour/tuples.md @@ -1,17 +1,75 @@ --- layout: tour -title: Tuples +title: 튜플 -discourse: true +discourse: false partof: scala-tour -num: + +num: 6 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 +스칼라에서 튜플은 고정된 수의 엘리먼트를 포함하는 값으로, 각 엘리먼트는 고유한 타입을 갖는다. 튜플은 변경이 불가능하다. + +튜플은 메서드에서 여러 개의 값을 반환할 때 특히 유용하다. + +두 개의 엘리먼트를 가진 튜플을 다음처럼 만들 수 있다. + +```tut +val ingredient = ("Sugar" , 25) +``` + +`String` 엘리먼트와 `Int` 엘리먼트로 이루어진 튜플이 생성된다. + +`ingredient`의 추론 타입은 `(String, Int)` 으로, `Tuple2[String, Int]`의 축약된 형식이다. + +튜플을 표현하기 위해, 스칼라는 `Tuple2`, `Tuple3` … `Tuple22`에 이르는 일련의 클래스를 사용한다. +각 클래스는 엘리먼트 개수와 같은 만큼의 타입 파라미터를 갖는다. + +## 엘리먼트 접근 + +엘리먼트에 접근하는 한 가지 방법은 위치 정보를 이용하는 것이다. 각각의 엘리먼트들은 `_1`, `_2` 와 같은 명칭을 갖는다. + +```tut +println(ingredient._1) // Sugar +println(ingredient._2) // 25 +``` +## 튜플의 패턴 매칭 +[패턴 매칭](pattern-matching.html)을 이용하여 튜플을 분리할 수 있다. + +```tut +val (name, quantity) = ingredient +println(name) // Sugar +println(quantity) // 25 +``` +여기서 `name`의 추론 타입은 `String`이고, `quantity`의 추론 타입은 `Int`이다. + +튜플 패턴 매칭의 또 다른 예는 다음과 같다. + +```tut +val planets = + List(("Mercury", 57.9), ("Venus", 108.2), ("Earth", 149.6), + ("Mars", 227.9), ("Jupiter", 778.3)) +planets.foreach{ + case ("Earth", distance) => + println(s"Our planet is $distance million kilometers from the sun") + case _ => +} +``` +혹은, `for` 구문 안에서 다음처럼 사용할 수 있다. + +```tut +val numPairs = List((2, 5), (3, -7), (20, 56)) +for ((a, b) <- numPairs) { + println(a * b) +} +``` + +## 튜플과 케이스 클래스 +사용자들은 때때로 튜플과 [케이스 클래스](case-classes.html) 중 하나를 선택하는 것이 어려울 수 있다. +케이스 클래스는 이름을 가진 엘리먼트다. 이름을 사용하면 몇몇 코드들의 가독성을 높일 수 있다. +위의 planet 예시에서는 튜플을 사용하는 것보다 `case class Planet(name: String, distance: Double)`을 정의하는 것이 더 나을 수 있다. \ No newline at end of file From d1507a51b60ab47ba3201867c98255b3e051be4b Mon Sep 17 00:00:00 2001 From: Horace Velmont Date: Wed, 3 Jul 2019 09:38:13 +0900 Subject: [PATCH 2/2] Unify code format with other ko-translation documents --- _ko/tour/tuples.md | 49 ++++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/_ko/tour/tuples.md b/_ko/tour/tuples.md index 85afa4d01d..e513922d6e 100644 --- a/_ko/tour/tuples.md +++ b/_ko/tour/tuples.md @@ -19,9 +19,7 @@ previous-page: traits 두 개의 엘리먼트를 가진 튜플을 다음처럼 만들 수 있다. -```tut -val ingredient = ("Sugar" , 25) -``` + val ingredient = ("Sugar" , 25) `String` 엘리먼트와 `Int` 엘리먼트로 이루어진 튜플이 생성된다. @@ -34,40 +32,35 @@ val ingredient = ("Sugar" , 25) 엘리먼트에 접근하는 한 가지 방법은 위치 정보를 이용하는 것이다. 각각의 엘리먼트들은 `_1`, `_2` 와 같은 명칭을 갖는다. -```tut -println(ingredient._1) // Sugar -println(ingredient._2) // 25 -``` + println(ingredient._1) // Sugar + println(ingredient._2) // 25 + ## 튜플의 패턴 매칭 [패턴 매칭](pattern-matching.html)을 이용하여 튜플을 분리할 수 있다. -```tut -val (name, quantity) = ingredient -println(name) // Sugar -println(quantity) // 25 -``` + val (name, quantity) = ingredient + println(name) // Sugar + println(quantity) // 25 + 여기서 `name`의 추론 타입은 `String`이고, `quantity`의 추론 타입은 `Int`이다. 튜플 패턴 매칭의 또 다른 예는 다음과 같다. -```tut -val planets = - List(("Mercury", 57.9), ("Venus", 108.2), ("Earth", 149.6), - ("Mars", 227.9), ("Jupiter", 778.3)) -planets.foreach{ - case ("Earth", distance) => - println(s"Our planet is $distance million kilometers from the sun") - case _ => -} -``` + val planets = + List(("Mercury", 57.9), ("Venus", 108.2), ("Earth", 149.6), + ("Mars", 227.9), ("Jupiter", 778.3)) + planets.foreach{ + case ("Earth", distance) => + println(s"Our planet is $distance million kilometers from the sun") + case _ => + } + 혹은, `for` 구문 안에서 다음처럼 사용할 수 있다. -```tut -val numPairs = List((2, 5), (3, -7), (20, 56)) -for ((a, b) <- numPairs) { - println(a * b) -} -``` + val numPairs = List((2, 5), (3, -7), (20, 56)) + for ((a, b) <- numPairs) { + println(a * b) + } ## 튜플과 케이스 클래스 사용자들은 때때로 튜플과 [케이스 클래스](case-classes.html) 중 하나를 선택하는 것이 어려울 수 있다.