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

Cats 타입 클래스

Functor, Monad 등을 알아봅니다

Cats 타입 클래스은(는) 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 Are Type Classes?

A type class is a pattern for ad-hoc polymorphism. It lets you add behavior to a type without modifying it. Cats is a Scala library that provides a rich hierarchy of type classes for functional programming.

  • Functor — things you can map over
  • Applicative — combine independent effects
  • Monad — sequence dependent effects

Functor: map

A Functor[F] provides map. It transforms the value inside a context without changing the context's structure.

import cats.Functor
import cats.instances.option._

val f = Functor[Option].map(Some(2))(_ + 1)
println(f) // Some(3)

Functor Laws

A lawful Functor must obey two laws:

  • Identity: fa.map(x => x) == fa
  • Composition: fa.map(f).map(g) == fa.map(f.andThen(g))

These guarantees let you refactor map chains safely.

Applicative: pure and ap

Applicative[F] extends Functor and adds pure (lift a value into the context) and the ability to combine independent values with mapN.

import cats.Applicative
import cats.instances.option._

val lifted = Applicative[Option].pure(42)
println(lifted) // Some(42)

Combining with mapN

The mapN syntax combines several independent effectful values into one, applying a function once all are present.

import cats.syntax.apply._
import cats.instances.option._

val result = (Option(1), Option(2), Option(3)).mapN(_ + _ + _)
println(result) // Some(6)

Monad: flatMap

Monad[F] extends Applicative and adds flatMap, which sequences dependent computations. Each step can depend on the result of the previous one.

import cats.Monad
import cats.instances.option._

val m = Monad[Option].flatMap(Some(3))(x => Some(x * 10))
println(m) // Some(30)

Monad Laws

Monads must obey three laws:

  • Left identity: pure(a).flatMap(f) == f(a)
  • Right identity: m.flatMap(pure) == m
  • Associativity: nested flatMaps can be re-grouped

Semigroup and Monoid

A Semigroup[A] defines combine (associative). A Monoid[A] adds an empty identity element. These power folding and accumulation.

import cats.syntax.semigroup._
import cats.instances.int._
import cats.instances.string._

println(3 |+| 4)       // 7
println("foo" |+| "bar") // foobar

Foldable

Foldable[F] abstracts folding over a structure. Combined with Monoid, combineAll reduces a collection to a single value.

import cats.Foldable
import cats.instances.list._
import cats.instances.int._

val total = Foldable[List].combineAll(List(1, 2, 3, 4))
println(total) // 10

Traverse

Traverse[F] lets you map each element to an effect and collect the results, turning List[F[A]] into F[List[A]].

import cats.syntax.traverse._
import cats.instances.list._
import cats.instances.option._

val r = List(1, 2, 3).traverse(x => Option(x * 2))
println(r) // Some(List(2, 4, 6))

Why Type Classes Matter

Type classes let you write code that works for any type providing the required behavior. A function constrained by Monad[F] works for Option, List, IO, and more — write once, reuse everywhere.

import cats.Monad
import cats.syntax.flatMap._
import cats.syntax.functor._

def twice[F[_]: Monad, A](fa: F[A])(f: A => A): F[A] =
  fa.flatMap(a => Monad[F].pure(f(f(a))))

Quick Check

Which type class adds flatMap for sequencing dependent computations?

Recap

You explored the core Cats type class hierarchy:

  • Functor — map
  • Applicative — pure and mapN
  • Monad — flatMap
  • Semigroup/Monoid — combine/empty
  • Foldable/Traverse — fold and collect effects

Next you'll meet the IO monad to model effects purely.

자주 묻는 질문

“Cats 타입 클래스” 강의는 무료인가요?

네 — “Cats 타입 클래스” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Scala for Backend Engineering & Functional Programming 강의 전체를 잠금 해제할 수 있습니다. Scala for Backend Engineering & Functional Programming 강의에는 총 4개의 강의가 포함되어 있습니다.

“Cats 타입 클래스”에서 뭘 배우나요?

Functor, Monad 등을 알아봅니다 브라우저에서 직접 실행하는 실습 코드로 Scala for Backend Engineering & Functional Programming을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Scala for Backend Engineering & Functional Programming을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Scala for Backend Engineering & Functional Programming은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.

“Cats 타입 클래스” 강의는 얼마나 걸리나요?

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

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

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

이 강의의 모든 강의

  1. Cats 타입 클래스
  2. IO 모나드
  3. IO 조합
  4. IO에서 오류 처리
← Scala for Backend Engineering & Functional Programming(으)로 돌아가기