设为首页 加入收藏

TOP

Scalaz(41)- Free :IO Monad-Free特定版本的FP语法(四)
2017-10-10 12:13:00 】 浏览:11094
Tags:Scalaz Free Monad 特定 版本 语法
toInt, dvsor.toInt).except(e => IO({println(e.getMessage());-99})).liftM[WriterTIO] 10 _ <- if (quot < 0) WriterT.writerT((List(s"divide by zero Error!!!"),-99).point[IO]) else putLn(s"the result:$quot").liftM[WriterTIO] 11 } yield (quot) 12 ... 13 object IOMonadDemo extends App { 14 import IOMonadPrg._ 15 // ioprg.unsafePerformIO() 16 //optionIOprg.run.unsafePerformIO() 17 println(writerIOprg.run.unsafePerformIO()) 18 ... 19 "enter dividend:" 20 3 21 "enter divisor:" 22 0 23 / by zero 24 (List(received dividend 3;, received divisor 0, ready to divide ..., divide by zero Error!!!),-99) 25 26 Process finished with exit code 0

 

以上例子调用了scalaz IO Monad typeclass 提供的except方法,scalaz还提供了其它的异常处理函数:

 

 /** Executes the handler if an exception is raised. */ def except(handler: Throwable => IO[A]): IO[A] = io(rw => try { Free.pure(this(rw).run) } catch { case e: Throwable => handler(e)(rw) }) /** * Executes the handler for exceptions that are raised and match the given predicate. * Other exceptions are rethrown. */ def catchSome[B](p: Throwable => Option[B], handler: B => IO[A]): IO[A] = except(e => p(e) match { case Some(z) => handler(z) case None => throw e }) /** * Returns a disjunction result which is right if no exception was raised, or left if an * exception was raised. */ def catchLeft: IO[Throwable \/ A] = map(\/.right[Throwable, A]) except (t => IO(-\/(t))) /**Like "catchLeft" but takes a predicate to select which exceptions are caught. */ def catchSomeLeft[B](p: Throwable => Option[B]): IO[B \/ A] = catchLeft map (_.leftMap(e => p(e).getOrElse(throw e))) /**Like "finally", but only performs the final action if there was an exception. */ def onException[B](action: IO[B]): IO[A] = this except (e => for { _ <- action a <- (throw e): IO[A] } yield a)

 

以下是这次讨论的完整示范源代码:

 1 package demo.app  2 
 3 import scalaz._  4 import Scalaz._  5 import effect._  6 import IO._  7 import Free._  8 import scala.language.higherKinds  9 import scala.language.implicitConversions 10 
11 object IOMonadPrg { 12   def div(dvdn: Int, dvsor: Int): IO[Int] =
13     IO(dvdn / dvsor) 14   val ioprg: IO[Int] = for { 15     _ <- putLn("enter dividend:") 16     dvdn <- readLn 17     _ <- putLn("enter divisor:") 18     dvsor <- readLn 19     quot <- div(dvdn.toInt, dvsor.toInt) 20     _ <- putLn(s"the result:$quot") 21   } yield quot 22   //implicit def ioToOptionT[A](io: IO[A]): OptionT[IO,A] = io.liftM[OptionT]
23   val optionIOprg: OptionT[IO,Int] = for { 24     _ <- putLn("enter dividend:").liftM[OptionT] 25     dvdn <- readLn.liftM[OptionT] 26     _ <- putLn("enter divisor:").liftM[OptionT] 27     dvsor <- readLn.liftM[OptionT] 28     a <- if (dvsor.toInt == 0 ) OptionT(IO(None: Option[String])) else IO(0).liftM[OptionT] 29     quot <- div(dvdn.toInt, dvsor.toInt).liftM[OptionT] 30     _ <- putLn(s"the result:$quot").liftM[OptionT] 31   } yield quot 32   type WriterTIO[F[_],A] = WriterT[F,List[String],A] 33   val writerIOprg: WriterT[IO,List[String],Int] = for { 34     _ <- putLn("enter dividend:").liftM[WriterTIO] 35     dvdn <- readLn.liftM[WriterTIO] 36     _ <- WriterT.writerT((List(s"received dividend $dvdn;"),dvdn).point[IO]) 37     _ <- putLn("enter divisor:").liftM[WriterTIO] 38     dvsor <- readLn.liftM[Wr
首页 上一页 1 2 3 4 5 下一页 尾页 4/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇PageRank基于Spark实现介绍 下一篇Scala学习手记1 - 快速体验

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目