Summary (บทสรุปย่อ)

  • functor คือการ implement map ที่รักษาโครงสร้างของ data type ไว้

  • กฎของ functor คือ

    • Identity: x.map(a => a) == x

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

  • monad คือการ implement ชุดเล็กที่สุดชุดใดชุดหนึ่งของ monadic combinators ที่เป็นไปตามกฎของ associativity และ identity

  • ชุดเล็กที่สุดของ monadic combinators คือ

    • unit และ flatMap

    • unit และ compose

    • unit , map และ join

  • กฎของ monad คือ

    • Associativity — x.flatMap(f).flatMap(g) == x.flatMap(a => f(a).flatMap(g))

    • Right identity — x.flatMap(unit) == x

    • Left identity — unit(y).flatMap(f) == f(y)

  • monads ทั้งหมดเป็น functors แต่ไม่ใช่ functors ทั้งหมดที่เป็น monads

  • มี monads สำหรับ data types หลายตัวที่พบในหนังสือเล่มนี้ รวมถึง Option , List , LazyList , Par และ State[S, _]

  • ข้อตกลงของ Monad ไม่ได้ระบุ ว่าอะไร กำลังเกิดขึ้นระหว่างบรรทัด มันระบุเพียงว่าไม่ว่าอะไรก็ตามที่ กำลัง เกิดขึ้นต้องเป็นไปตามกฎของ associativity และ identity

  • การให้ Monad instance สำหรับ type constructor มีประโยชน์ในทางปฏิบัติ การทำเช่นนั้นทำให้เข้าถึง derived operations (หรือ combinators) ทั้งหมดเป็นการแลกเปลี่ยนกับการ implement ชุดเล็กที่สุดชุดใดชุดหนึ่งของ monadic combinators