Skip to content

Instantly share code, notes, and snippets.

@rbonvall
Created May 18, 2016 18:02
Show Gist options
  • Save rbonvall/01bae32c3857408ef47808a2016199e3 to your computer and use it in GitHub Desktop.
Save rbonvall/01bae32c3857408ef47808a2016199e3 to your computer and use it in GitHub Desktop.
//////////////////////////////////////////////////
// //
// El patrón del match //
// //
// Roberto Bonvallet //
// @rbonvall //
// //
// Martes 17 de mayo de 2016 //
// Santiago Scala Meetup //
// //
//////////////////////////////////////////////////
val x = readInt()
x match {
case 0 => println("cero")
case 1 => println("uno")
case _ => println("otro")
}
val x = readInt()
val msg = x match {
case 0 => "cero"
case 1 => "uno"
case _ => "otro"
}
println(msg)
val x = readInt()
val msg = x match {
case 0 => "cero"
case 1 => "uno"
case n => s"el $n"
}
println(msg)
def isPrime(n: Int): Boolean = (2 until n).forall(n % _ != 0)
val x = readInt()
val msg = x match {
case 0 => "cero"
case 1 => "uno"
case n if isPrime(n) => s"el primo $n"
case n => s"el $n"
}
println(msg)
case class Date(year: Int, month: Int, day: Int)
val d: Date = today()
val msg = d match {
case Date(2016, 5, 17) => "meetup de Scala"
case Date(y, 12, 25) => s"navidad del $y"
case Date(2016, _, _) => "este año"
case Date(_, 12 | 1 | 2, _) => "día caluroso"
case Date(y, _, _) if y < 2016 => "el pasado"
}
case class Date(year: Int, month: Int, day: Int) {
def next: Date = this match {
case Date(y, 12, 31) ⇒ Date(y + 1, 1, 1)
case Date(y, 2, 28) if isLeap(y) ⇒ Date(y, 2, 29)
case Date(y, 2, 29) if isLeap(y) ⇒ Date(y, 3, 1)
case Date(y, m, d) if d == days(m) ⇒ Date(y, m + 1, 1)
case Date(y, m, d) ⇒ Date(y, m, d + 1)
}
}
val days = Array(0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31)
def isLeap(year: Int) = year % 4 == 0 &&
year % 100 != 0 ||
year % 400 == 0
case class Point(x: Double, y: Double)
case class Segment(p: Point, q: Point)
def length(s: Segment): Double = {
val Segment(Point(x1, y1), Point(x2, y2)) = s
val (dx, dy) = (x2 - x1, y2 - y1)
Math.sqrt(dx * dx + dy * dy)
}
sealed trait Expr
case object Pi extends Expr
case class Num(x: Double) extends Expr
case class Plus(l: Expr, r: Expr) extends Expr
case class Times(l: Expr, r: Expr) extends Expr
case class FuncCall(name: String,
args: List[Expr)) extends Expr
def eval(expr: Expr): Double = expr match {
case Pi =>
3.141592653589793
case Num(x) =>
x
case Plus (l, r) =>
eval(l) + eval(r)
case Times(Num(0), _) | Times (_, Num(0)) =>
0
case Times(l, r) =>
eval(l) * eval(r)
case FuncCall("SQRT", List(x)) =>
Math.sqrt(eval(x))
case FuncCall("MIN", args) =>
args.map(eval).reduce(Math.min)
case FuncCall("IF", List(c, t, f)) =>
val cond = eval(c) != 0
if (cond) eval(t) else eval(f)
case FuncCall(f, args) =>
val n = args.length
val msg = s"$f with $n args not supported"
throw new Exception(msg)
}
val rut = """^(\d+)-([0-9k])$""".r
println(
readLine() match {
case rut(num, dv) => s"$num raya $dv"
case _ => "eso no parece un rut"
}
)
val line1 = "I 0.0 0.0 1 3"
val line2 = "M -1.0 1.50 7 15"
val pattern =
"""(I|M) (-?\d+\.\d+) (-?\d+\.\d+) (\d+) (\d+)""".r
def parse(line: String) = line match {
case pattern("I", x, y, n, m) =>
doSomething(Inch, x.toDouble, y.toDouble, n.toInt, m.toInt)
case pattern("M", x, y, n, m) =>
doSomething(Mm, x.toDouble, y.toDouble, n.toInt, m.toInt)
}
object d {
def unapply(s: String): Option[Double] =
Try(s.toDouble).toOption
}
object i {
def unapply(s: String): Option[Int] =
Try(s.toInt).toOption.filter(_ >= 0)
}
object u {
def unapply(s: String): Option[Unit] =
if (s == "I") Some(Inch)
else if (s == "M") Some(Mm)
else None
}
}
def parse(line: String) = line.split(" ") match {
case Array(u(z), d(x), d(y), i(n), i(m)) =>
doSomething(z, x, y, n, m)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment