设为首页 加入收藏

TOP

Cats(3)- freeK-Free编程更轻松,Free programming with freeK(三)
2017-10-10 12:11:53 】 浏览:6691
Tags:Cats freeK-Free 编程 轻松 Free programming with freeK
prompt: String) extends Interact[String]
11 case class Tell(msg: String) extends Interact[Unit] 12 } 13 sealed trait Login[+A] 14 object Login { 15 case class Authenticate(uid: String, pwd: String) extends Login[Boolean] 16 } 17 sealed trait Auth[+A] 18 object Auth { 19 case class Authorize(uid: String) extends Auth[Boolean] 20 } 21 } 22 object DSLs { 23 import ADTs._ 24 import Interact._ 25 import Login._ 26 type PRG = Interact :|: Login :|: NilDSL 27 val PRG = DSL.Make[PRG] 28 29 val authenticDSL: Free[PRG.Cop, Boolean] = 30 for { 31 uid <- Ask("Enter your user id:").freek[PRG] 32 pwd <- Ask("Enter password:").freek[PRG] 33 auth <- Authenticate(uid,pwd).freek[PRG] 34 } yield auth 35 36 val interactLoginDSL: Free[PRG.Cop, Unit] = 37 for { 38 uid <- Ask("Enter your user id:").freek[PRG] 39 pwd <- Ask("Enter password:").freek[PRG] 40 auth <- Authenticate(uid,pwd).freek[PRG] 41 _ <- if (auth) Tell(s"Hello $uid, welcome to the zoo!").freek[PRG] 42 else Tell(s"Sorry, Who is $uid?").freek[PRG] 43 } yield () 44 45 import Auth._ 46 type PRG3 = Auth :|: PRG //Interact :|: Login :|: NilDSL 47 val PRG3 = DSL.Make[PRG3] 48 val authorizeDSL: Free[PRG3.Cop, Unit] = 49 for { 50 uid <- Ask("Enter your User ID:").freek[PRG3] 51 pwd <- Ask("Enter your Password:").freek[PRG3] 52 auth <- Authenticate(uid,pwd).freek[PRG3] 53 perm <- if (auth) Authorize(uid).freek[PRG3] 54 else Free.pure[PRG3.Cop,Boolean](false) 55 _ <- if (perm) Tell(s"Hello $uid, access granted!").freek[PRG3] 56 else Tell(s"Sorry $uid, access denied!").freek[PRG3] 57 } yield() 58 59 } 60 object IMPLs { 61 import ADTs._ 62 import Interact._ 63 import Login._ 64 val idInteract = new (Interact ~> Id) { 65 def apply[A](ia: Interact[A]): Id[A] = ia match { 66 case Ask(p) => {println(p); scala.io.StdIn.readLine} 67 case Tell(m) => println(m) 68 } 69 } 70 val idLogin = new (Login ~> Id) { 71 def apply[A](la: Login[A]): Id[A] = la match { 72 case Authenticate(u,p) => (u,p) match { 73 case ("Tiger","123") => true 74 case _ => false 75 } 76 } 77 } 78 val interactLogin = idInteract :&: idLogin 79 import Dependencies._ 80 type ReaderContext[A] = Reader[Authenticator,A] 81 object readerInteract extends (Interact ~> ReaderContext) { 82 def apply[A](ia: Interact[A]): ReaderContext[A] = ia match { 83 case Ask(p) => Reader {pc => {println(p); scala.io.StdIn.readLine}} 84 case Tell(m) => Reader {_ => println(m)} 85 } 86 } 87 object readerLogin extends (Login ~> ReaderContext) { 88 def apply[A](la: Login[A]): ReaderContext[A] = la match { 89 case Authenticate(u,p) => Reader {pc => pc.matchUserPassword(u,p)} 90 } 91 } 92 val userInteractLogin = readerLogin :&: readerInteract 93 94 val readerAuth = new (Auth ~> ReaderContext) { 95 def apply[A](aa: Auth[A]): ReaderContext[A] = aa match { 96 case Authorize(u) => Reader {ac => ac.grandAccess(u)} 97 } 98 } 99 val userAuth = readerAuth :&: userInteractLogin 100 } 101 102 } 103 object Dependencies { 104 trait PasswordControl { 105 val mapPasswords: Map[String,String] 106 def matchUserPassword(uid: String, pswd: String): Boolean 107 } 108 trait AccessControl { 109 val mapAccesses: Map[String, Boolean] 110 def grandAccess(uid: String): Boolean 111 } 112 trait Authenticator extends PasswordControl with AccessControl 113 } 1
首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Scala--高阶函数 下一篇scala学习手记36 - 容器基础

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目