+关注继续查看

1 sealed trait Quiz[+Next]
2 case class Question[Next](que: String, n: String => Next) extends Quiz[Next]
3 case class Answer[Next](ans: String, n: Next) extends Quiz[Next]

1   implicit object QFunctor extends Functor[Quiz] {
2     def map[A,B](qa: Quiz[A])(f: A => B): Quiz[B] =
3       qa match {
4          case q: Question[A] => Question(q.que, q.n andThen f)
6       }
7   }

1 //操作帮助方法helper methods
2   def askNumber(q: String) = Question(q, (inputString => inputString.toInt))  //_.toInt
4   def answer(fnum: Int, snum: Int, opr: Char) = {
5     def result =
6       opr match {
7         case 'A' => fnum + snum
8         case 'M' => fnum * snum
9         case 'D' => fnum / snum
10         case 'S' => fnum - snum
11       }
13   }

1 import Quiz._
2 val prg = for {
3  fn <- askNumber("The first number is:")
4  sn <- askNumber("The second number is:")
5  op <- askOperator("The operation is:")
7 } yield()                                         //> prg  : scalaz.Free[Exercises.interact.Quiz,Unit] = Gosub()

1   implicit def quizToFree[A](qz: Quiz[A]): Free[Quiz,A] = Free.liftF(qz)

 1 sealed trait Quiz[+Next]
2 object Quiz {
3 //问题que:String, 等待String 然后转成数字或操作符号
4   case class Question[Next](que: String, n: String => Next) extends Quiz[Next]
5   case class Answer[Next](ans: String, n: Next) extends Quiz[Next]
6   implicit object QFunctor extends Functor[Quiz] {
7     def map[A,B](qa: Quiz[A])(f: A => B): Quiz[B] =
8       qa match {
9          case q: Question[A] => Question(q.que, q.n andThen f)
11       }
12   }
13 //操作帮助方法helper methods
14   def askNumber(q: String) = Question(q, (inputString => inputString.toInt))  //_.toInt
16   def answer(fnum: Int, snum: Int, opr: Char) = {
17     def result =
18       opr match {
19         case 'A' => fnum + snum
20         case 'M' => fnum * snum
21         case 'D' => fnum / snum
22         case 'S' => fnum - snum
23       }
25   }
26   implicit def quizToFree[A](qz: Quiz[A]): Free[Quiz,A] = Free.liftF(qz)
27 }
28 import Quiz._
29 val prg = for {
30  fn <- askNumber("The first number is:")
31  sn <- askNumber("The second number is:")
32  op <- askOperator("The operation is:")
34 } yield()                                         //> prg  : scalaz.Free[Exercises.interact.Quiz,Unit] = Gosub()

 1 sealed trait Calc[+A]
2 object Calc {
3   case class Push(value: Int) extends Calc[Unit]
4   case class Add() extends Calc[Unit]
5   case class Mul() extends Calc[Unit]
6   case class Div() extends Calc[Unit]
7   case class Sub() extends Calc[Unit]
8   implicit def calcToFree[A](ca: Calc[A]) = Free.liftFC(ca)
9 }
10 import Calc._
11 val ast = for {
12   _ <- Push(23)
13   _ <- Push(3)
15   _ <- Push(5)
16   _ <- Mul()
17 } yield ()                                        //> ast  : scalaz.Free[[x]scalaz.Coyoneda[Exercises.interact.Calc,x],Unit] = Gosub()

LeetCode 150. Evaluate Reverse Polish Notation

9 0
CodeForces - 1485D Multiples and Power Differences （构造+lcm）
CodeForces - 1485D Multiples and Power Differences （构造+lcm）
13 0
Data Structures and Algorithms (English) - 6-1 Deque（25 分）
Data Structures and Algorithms (English) - 6-1 Deque（25 分）
33 0
Data Structures and Algorithms (English) - 6-2 Two Stacks In One Array（20 分）
Data Structures and Algorithms (English) - 6-2 Two Stacks In One Array（20 分）
54 0
LeetCode 150：逆波兰表达式求值 Evaluate Reverse Polish Notation

1293 0
+关注

scala functional programming enthusiast