튜플 구조 분해
값을 다시 꺼내 보세요.
튜플 구조 분해은(는) CoddyKit의 무료 Scala for Backend Engineering & Functional Programming 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Scala for Backend Engineering & Functional Programming 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Scala for Backend Engineering & Functional Programming 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Getting Values Out
Creating a tuple is only half the story. To use the values inside, you need to take them back out.
Scala gives you two main ways: numbered accessors and pattern destructuring. This lesson covers both.
Accessing by Position
Every tuple element has a position you can read with ._1, ._2, and so on.
Note that positions start at one, not zero. So ._1 is the first element.
val person = ("Alice", 30)
println(person._1)
println(person._2)Using Accessors in Code
Positional accessors are handy when you need just one element.
Here we read the age from a pair and add one to it. Accessors return the original element type, so math works directly.
val record = ("Bob", 42)
val nextYear = record._2 + 1
println(nextYear)Destructuring with val
Reading ._1 and ._2 can get hard to follow. Destructuring lets you name each element at once.
This val pattern binds name and age in a single line.
val (name, age) = ("Cara", 25)
println(name)
println(age)Why Destructuring Helps
Named variables read far better than numbered positions.
Comparing age to person._2, the destructured version makes your intent obvious and avoids off-by-one mistakes.
val (city, population) = ("Lyon", 500000)
println(s"$city has $population people")Destructuring Three Elements
Destructuring works for tuples of any size, not just pairs.
Here a three-element tuple is unpacked into three named variables in one statement.
val (id, label, active) = (7, "task", true)
println(id)
println(label)
println(active)Ignoring With Underscore
Sometimes you only want some of the values. Use an underscore _ to skip a position.
Here we keep just the name and ignore the age completely.
val (name, _) = ("Dana", 40)
println(name)Destructuring Function Results
Functions that return tuples pair naturally with destructuring.
You can name both returned values right where you call the function, making the result self-documenting.
def minMax(a: Int, b: Int) = (a min b, a max b)
val (low, high) = minMax(8, 3)
println(low)
println(high)Swapping a Pair
A two-element tuple has a built-in swap method that flips its elements.
This returns a new pair with the order reversed, which is occasionally handy.
val pair = ("a", 1)
println(pair.swap)Destructuring in for
When looping over a collection of pairs, you can destructure inside the loop header.
Each pair is unpacked into key and value automatically on every iteration.
val pairs = List(("a", 1), ("b", 2))
for ((key, value) <- pairs) println(s"$key=$value")Types Are Preserved
Destructuring never loses type information. Each named variable keeps the exact type of its slot.
So in val (n, age) = ("Eve", 33), n is a String and age is an Int, ready for type-safe use.
val (n, age) = ("Eve", 33)
println(n.length)
println(age * 2)Quick Check
Test what you learned about destructuring tuples.
Recap
You can pull tuple values out with positional accessors like ._1 (one-based) or, more clearly, with destructuring such as val (a, b) = pair.
Underscore skips unwanted slots, destructuring works in for loops, and types are always preserved. Next you will explore ranges.
자주 묻는 질문
“튜플 구조 분해” 강의는 무료인가요?
네 — “튜플 구조 분해” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Scala for Backend Engineering & Functional Programming 강의 전체를 잠금 해제할 수 있습니다. Scala for Backend Engineering & Functional Programming 강의에는 총 4개의 강의가 포함되어 있습니다.
“튜플 구조 분해”에서 뭘 배우나요?
값을 다시 꺼내 보세요. 브라우저에서 직접 실행하는 실습 코드로 Scala for Backend Engineering & Functional Programming을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Scala for Backend Engineering & Functional Programming을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Scala for Backend Engineering & Functional Programming은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.
“튜플 구조 분해” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Scala for Backend Engineering & Functional Programming 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Scala for Backend Engineering & Functional Programming 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 튜플 만들기
- 튜플 구조 분해
- 범위 만들기
- 반복문에서 범위 사용하기