ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Writer
    scala/cats2 2023. 1. 19. 20:52

    Writer Monad 예제

    import cats.data._
    import cats.syntax.all._
    
    object WriterTest {
      def main(args: Array[String]): Unit = {
        val x: Writer[List[String], Int] = 1.writer(List("one"))
        val y: Writer[List[String], Int] = 23.writer(List("two", "three"))
    
        val z: Writer[List[String], Int] = for {
          a <- x
          b <- y
        } yield a + b
    
        val result: (List[String], Int) = z.run
        println(result)
      }
    }

     

    출력 결과

    (List(one, two, three),24)

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

    Cats2 Exercise  (0) 2023.01.22
    Validated  (0) 2023.01.19
    OptionT  (0) 2023.01.16
    Nested  (0) 2023.01.16
    State  (0) 2023.01.10

    댓글

Designed by Tistory.