scala/cats2

Writer

wefree 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)