0Pricing
Scala for Backend Engineering & Functional Programming · 강의

튜플 만들기

관련된 값들을 함께 묶어 보세요.

튜플 만들기은(는) CoddyKit의 무료 Scala for Backend Engineering & Functional Programming 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Scala for Backend Engineering & Functional Programming 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Scala for Backend Engineering & Functional Programming 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

What Is a Tuple?

A tuple groups a fixed number of values together, even when those values have different types.

Unlike a list, where every element shares one type, a tuple can hold an Int, a String, and a Boolean all at once.

Tuples are perfect for returning or bundling a handful of related values without writing a whole class.

Creating Your First Tuple

You build a tuple by wrapping comma-separated values in parentheses.

Here we pair a name with an age. Scala figures out the types automatically.

val person = ("Alice", 30)
println(person)

Mixing Types Freely

One of the best features of tuples is mixing types in a single value.

This tuple holds a String, an Int, and a Boolean together. Each slot keeps its own type.

val record = ("Bob", 42, true)
println(record)

Tuple Types

The type of a tuple shows every element type in order.

A pair of a String and an Int has type (String, Int). You can write the type explicitly if you want to be clear.

val pair: (String, Int) = ("Cara", 25)
println(pair)

Pairs Are Common

A tuple with exactly two elements is called a pair, and pairs appear everywhere in Scala.

For example, maps are built from key-value pairs. Learning pairs first makes the rest of the language easier.

val score = ("Math", 95)
println(score)

Arrow Syntax for Pairs

For two-element tuples, Scala offers a handy arrow syntax.

Writing key -> value is exactly the same as (key, value). It reads nicely, especially when building maps.

val entry = "name" -> "Dana"
println(entry)

Larger Tuples

Tuples can hold more than two or three values.

Scala supports tuples up to 22 elements, though small tuples are easiest to read. If you need many fields, a case class is usually clearer.

val data = (1, 2, 3, 4, 5)
println(data)

Single-Element Note

There is no useful one-element tuple. Putting one value in parentheses just gives you that value back.

So (5) is simply the number 5, not a tuple. Tuples really start at two elements.

val notATuple = (5)
println(notATuple)

Tuples Are Immutable

Once you create a tuple, its values never change.

This immutability makes tuples safe to share and easy to reason about. To get a different tuple, you simply build a new one.

val a = (1, "one")
val b = (2, "two")
println(a)
println(b)

Returning Multiple Values

A common use of tuples is returning several values from a function at once.

Here a function returns both a sum and a product as a single tuple, avoiding the need for a separate class.

def stats(a: Int, b: Int) = (a + b, a * b)
println(stats(3, 4))

Nesting Tuples

Because a tuple is just a value, you can place a tuple inside another tuple.

This lets you group related sub-pairs together. Keep nesting shallow so your code stays readable.

val nested = (("x", 1), ("y", 2))
println(nested)

Quick Check

Test what you learned about creating tuples.

Recap

You learned that tuples group a fixed number of values of possibly different types inside parentheses.

Pairs can also use key -> value syntax, tuples are immutable, and they are great for returning multiple values. Next you will learn how to pull those values back out.

자주 묻는 질문

“튜플 만들기” 강의는 무료인가요?

네 — “튜플 만들기” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 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개 중 1번째 강의입니다.

“튜플 만들기” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Scala for Backend Engineering & Functional Programming 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Scala for Backend Engineering & Functional Programming 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 튜플 만들기
  2. 튜플 구조 분해
  3. 범위 만들기
  4. 반복문에서 범위 사용하기
← Scala for Backend Engineering & Functional Programming(으)로 돌아가기