首页 > 解决方案 > 使用 Monad 处理副作用

问题描述

在下面的程序中,我无法理解 map 和 flatMap 实现背后的思考过程。这就是为什么我无法考虑在不同类型的副作用的情况下如何实现 map 和 flatMap,例如 print 语句等。

有人可以解释一下吗?

import java.util.UUID

object MonadSideEffectToPure extends App {

  case class User(id: UUID, email: String)
  case class Address(id: Int, city: String, country: String)
  case class Email(to: String, subject: String, body: String)

  def findUser(id: UUID): User = {
    //perform database operations and return user
    User(id, "test@gmail.com")
  }

  def getAddress(user: User): Address = {
    //perform database operation and return address
    Address(1, "haryana", "India")
  }

  def sendEmail(email: Email) = {
    //send an email using third party service
    println(s"email sent to ${email.to} ...")
  }

  //Create IO Monad
  class IO[A] private(block: => A) {
    def run : A = block
    def flatMap[B] (f:A => IO[B]): IO[B] = IO {f(run).run}
    def map[B] (f: A => B): IO[B] = flatMap(a => IO{f(a)})
  }

  object IO {
     def apply[A](block: => A): IO[A] = new IO(block)
  }

  val result: IO[Unit] = for {
    user <-  IO { findUser(UUID.randomUUID())}
    address <- IO {getAddress(user)}
    _ <- IO {sendEmail(Email(user.email, address.city + "News", "Good News"))}
  } yield()

  result.run
}

标签: scalamonads

解决方案


推荐阅读