ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • kittens example
    scala/cats2 2025. 3. 5. 20:56

    https://github.com/typelevel/kittens

     

    "org.typelevel" %% "kittens" % "3.4.0"

     

    Example1

    import cats.Show
    import cats.derived.semiauto
    import cats.implicits.*
    
    case class Name(value: String)
    case class Person(name: Name, age: Int)
    
    object Person {
      given Show[Person] = semiauto.show
    }
    
    object Test {
      def main(args: Array[String]): Unit = {
        val person = Person(Name("KJM"), 20)
        println(person.show)  // Person(name = Name(value = KJM), age = 20)
      }
    }

     

    Example2

    import cats.Functor
    import cats.derived.semiauto
    import cats.implicits.*
    
    case class Content[T](items: Seq[T])
    
    object Content {
      given Functor[Content] = semiauto.functor
    }
    
    object Test {
      def main(args: Array[String]): Unit = {
        val content = Content[Int](List(1, 2))
        println(content.map(_ + 1)) // Content(List(2, 3))
      }
    }

     

    'scala > cats2' 카테고리의 다른 글

    Eval and Trampolining  (0) 2023.07.29
    Future Traverse  (0) 2023.02.13
    Ior  (1) 2023.01.29
    Cats2 Exercise  (0) 2023.01.22
    Validated  (0) 2023.01.19

    댓글

Designed by Tistory.