Skip to content

Instantly share code, notes, and snippets.

View seanparsons's full-sized avatar
💭
Setting A Status Message

Sean Parsons seanparsons

💭
Setting A Status Message
View GitHub Profile
String intMatch(int number) {
switch (number) {
case 1: return "This is number 1.";
case 2: return "This is number 2, it comes after 1.";
default: return "This number is neither 1 nor 2.";
}
}
def intMatch(number: Int): String = {
return number match {
case 1 => "This is number 1."
case 2 => "This is number 2, it comes after 1."
case _ => "This number is neither 1 nor 2."
}
}
def anyMatch(something: Any): String = {
return something match {
case text: String => text
case number: Int => "This is the number " + number + "."
case _ => "Unknown thing."
}
}
def variableMatch(expectedNumber: Int, numberGiven: Int): String = {
return numberGiven match {
case `expectedNumber` => "Wahoo!"
case _ => "Doh!"
}
}
def positiveIntMatch(number: Int): String = {
return number match {
case positive: Int if positive >= 0 => positive + " is greater than or equal to zero."
case negative: Int => negative + " is less than zero."
}
}
case class ChocolateCake(val chocolatey: Int) extends Food
case class ShotFired(val playerName: String, val atX: Int, val y: Int)
val result = new ShotFired("Sean", 2, 5) match {
case ShotFired(by, atX, atY) => by + " fired at (" + atX + "," + atY + ")"
}
case ShotFired(by, atX, atY) => by + " fired at (" + atX + "," + atY + ")"
case class MedikitUsed(val name: String, val useAmount: Int)
val result = new MedikitUsed("Sean", 0) match {
case MedikitUsed(name, 0) => name + " attempted to use none of their medikit and that makes no sense."
}
abstract class SpecialWeapon
case class SpecialWeaponUsed(val player: String, val weapon: SpecialWeapon)
case class NuclearBomb(val megatons: Int) extends SpecialWeapon
val result = new SpecialWeaponUsed("Sean", new NuclearBomb(17)) match {
case SpecialWeaponUsed(name, NuclearBomb(megatons)) => name + " used a " + megatons + " megaton nuclear bomb."
}