Skip to content

Instantly share code, notes, and snippets.

@denismaster
Last active November 14, 2017 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denismaster/12d360453277bde4e6c232cdb71b7852 to your computer and use it in GitHub Desktop.
Save denismaster/12d360453277bde4e6c232cdb71b7852 to your computer and use it in GitHub Desktop.
ПСЯПР четвертая
object Main extends App {
def processList1(list: List[(Int, Int)]): List[Option[Double]] =
list.map(elem => {
val (c, z) = elem
z match {
case 0 => None
case _ => Some(c*1.0/z)
}
})
val result = processList1(List((1, 1), (2, 1), (1, 3), (0, 1), (5, 0)))
println(result)
def processList2(list: List[Option[Double]]): List[String] =
list.map {
case Some(s) => "Результат деления=" + s
case None => "Деление на 0 невозможно"
}
println(processList2(result))
abstract class Tree
case class Leaf(operand:Int) extends Tree
case class BinaryOperation(operation: String, left:Tree, right:Tree) extends Tree
case class UnaryOperation(operation: String, operand: Tree) extends Tree
val tree = BinaryOperation("+",
BinaryOperation(
"*",
Leaf(2),
Leaf(4)
),
BinaryOperation(
"/",
Leaf(6),
Leaf(2)
)
)
val tree2 = UnaryOperation("++", Leaf(6))
def syntaxTreeToRPN(tree: Tree): String = {
tree match {
case Leaf(value) => value.toString
case UnaryOperation(operation, operand) =>
syntaxTreeToRPN(operand) + " "+ operation
case BinaryOperation(operation, left, right)=>
syntaxTreeToRPN(left) + " " + syntaxTreeToRPN(right) + " "+ operation
case _ => ""
}
}
println(syntaxTreeToRPN(tree))
println(syntaxTreeToRPN(tree2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment