scala - 使用 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
}
解决方案
推荐阅读
- validation - 为什么将错误类型分配给模型属性时猫鼬不会抛出?
- java - 由 Iterables.partition() 生成的拆分器没有按预期运行?
- c++ - C++ 类问题设置点在类请
- python - PySimpleGUI - 列不会更新
- ubuntu - strace: exec: Permission denied (error), 在 linux 中尝试 strace 时。我该如何解决?
- php - 通过检查值 php 删除数组元素
- rust - Rust 编译错误 - 无法推断类型参数的类型
- windows - Hashicorp Packer 在 Vsphpere-iso 中创建 windows 2019 模板
- wordpress - 自定义分类突然重定向到另一个页面
- dialogflow-es - DialogFlow:从文档中导入训练短语而不是手动输入?