Skip to content

Instantly share code, notes, and snippets.

@mayonesa
Created August 11, 2019 21:34
Show Gist options
  • Save mayonesa/997378d1a0a9949e29dfd1f99da31fd1 to your computer and use it in GitHub Desktop.
Save mayonesa/997378d1a0a9949e29dfd1f99da31fd1 to your computer and use it in GitHub Desktop.
import cats.data.State
import cats.syntax.applicative._ // for pure
object PostOrderCalc {
type CalcState[A] = State[List[Int], A]
def evalInput(str: String): Int =
evalAll(str.split(" ").toList).runA(Nil).value
def evalAll(input: List[String]): CalcState[Int] =
input.foldLeft(0.pure[CalcState]) { (acc, str) =>
acc.flatMap(_ => evalOne(str))
}
def evalOne(sym: String): CalcState[Int] =
State {
sym match {
case "+" => operator(_ + _)
case "_" => operator(_ - _)
case "*" => operator(_ * _)
case "/" => operator(_ / _)
case _ => stack =>
val int = sym.toInt
(int :: stack, int)
}
}
private def operator(op: (Int, Int) => Int): List[Int] => (List[Int], Int) = {
case a :: b :: rest =>
val res = op(a, b)
(res :: rest, res)
case _ =>
sys.error("Fail!")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment