设为首页 加入收藏

TOP

Scalaz(12)- Monad:再述述flatMap,顺便了解MonadPlus(四)
2017-10-10 12:13:33 】 浏览:2447
Tags:Scalaz Monad flatMap 顺便 了解 MonadPlus
: String): KeyLog[K] = new KeyLog[K] {
22 def value = k 23 def log = msg 24 } */ 25 implicit object keylogMonad extends Monad[KeyLog] { 26 def point[K](k: => K): KeyLog[K] = KeyIn(k,"") 27 def bind[K,I](kk: KeyLog[K])(f: K => KeyLog[I]): KeyLog[I] = kk flatMap f 28 } 29 }

我们增加了KeyIn和KeyLock两种状态。然后我们只需要通过模式匹配(pattern matching)在实现前面逻辑的时候把多种KeyLog状态考虑进去。

运行前面的例子:

 1 def enterInt(k: Int): KeyLog[Int] = KeyIn(k, "Number:"+k.toString)  2                                                   //> enterInt: (k: Int)Exercises.keylog.KeyLog[Int]
 3 def enterStr(k: String): KeyLog[String] = KeyIn(k,"String:"+k)  4                                                   //> enterStr: (k: String)Exercises.keylog.KeyLog[String]
 5 enterInt(3) >>= {a => enterInt(4) >>= {b => enterStr("Result:") map {c => c + (a * b).toString} }}  6                                                   //> res0: Exercises.keylog.KeyLog[String] = [Result:12,Number:3;Number:4;String  7                                                   //| :Result:;]
 8 for {  9  a <- enterInt(3) 10  b <- enterInt(4) 11  c <- enterStr("Result:") 12 } yield c + (a * b).toString                      //> res1: Exercises.keylog.KeyLog[String] = [Result:12,Number:3;Number:4;String 13                                                   //| :Result:;]

 现在把KeyLock效果加进去:

1 enterInt(3) >>= {a => (KeyLock: KeyLog[Int]) >>= {b => enterStr("Result:") map {c => c + (a * b).toString} }}
2                                                   //> res2: Exercises.keylog.KeyLog[String] = [Keypad Locked]
3 for {
4  a <- enterInt(3)
5  b <- enterInt(4)
6  x <- (KeyLock: KeyLog[String])
7  c <- enterStr("Result:")
8 } yield c + (a * b).toString                      //> res3: Exercises.keylog.KeyLog[String] = [Keypad Locked]

正是我们期待的效果。

现在我们可以把MonadPlus特质混入keylogMonad实例(trait mix-in):

 

 1     implicit object keylogMonad extends Monad[KeyLog] with MonadPlus[KeyLog] {  2         def point[K](k: => K): KeyLog[K] = KeyIn(k,"")  3         def bind[K,I](kk: KeyLog[K])(f: K => KeyLog[I]): KeyLog[I] = kk flatMap f  4 
 5         def empty[K]: KeyLog[K] = KeyLock  6         def plus[K](a: KeyLog[K], b: => KeyLog[K]): KeyLog[K] = a match {  7             case KeyIn(value,log) => KeyIn(value,log)  8             case KeyLock => b  9  } 10     }

 

在实例中我们实现了empty和plus。

那么现在我们可以使用守卫函数了吧:

 1 for {  2  a <- enterInt(3)  3  b <- enterInt(4)  4  c <- enterStr("Result:")  5 } yield c + (a * b).toString                      //> res3: Exercises.keylog.KeyLog[String] = [Result:12,Number:3;Number:4;String  6                                                   //| :Result:;]
 7 for {  8  a <- enterInt(3)  9  b <- enterInt(4) if b > 0
10  c <- enterStr("Result:") 11 } yield c + (a * b).toString                      //> res4: Exercises.keylog.KeyLog[String] = [Result:12,Number:3;Number:4;;Strin 12                                                   //| g:Result:;]
13 for { 14  a <- enterInt(3) 15  b <- enterInt(4) if b > 5
16  c <- enterStr("Result:") 17 } yield c + (a * b).toString                      //> res5: Exercises.keylog.KeyLog[String] = [Keypad Locked]

守卫函数按要求对KeyLog状态进行了过滤。

 

 

 

 

 

 

 

 

首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇spark视频课程下载链接 下一篇Scalaz(13)- Monad:Writer - ..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目