0Pricing
Scala for Backend Engineering & Functional Programming · درس

Type Classes في Cats

Functor وMonad وغيرهما

Type Classes في Cats درس مجاني في Scala for Backend Engineering & Functional Programming على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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.

الأسئلة الشائعة

هل درس «Type Classes في Cats» مجاني؟

نعم — نص درس «Type Classes في Cats» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Scala for Backend Engineering & Functional Programming، انتقل إلى CoddyKit PRO. تتضمن دورة Scala for Backend Engineering & Functional Programming 4 دروس في المجموع.

ماذا ستتعلم في «Type Classes في Cats»؟

Functor وMonad وغيرهما تتمرن على Scala for Backend Engineering & Functional Programming مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Scala for Backend Engineering & Functional Programming؟

لا تُشترط خبرة سابقة. Scala for Backend Engineering & Functional Programming على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «Type Classes في Cats»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Scala for Backend Engineering & Functional Programming هذا؟

نعم. كل درس في Scala for Backend Engineering & Functional Programming يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. Type Classes في Cats
  2. Monad الخاص بـIO
  3. تركيب IO
  4. معالجة الأخطاء في IO
← العودة إلى Scala for Backend Engineering & Functional Programming