Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- case class Person(name: String)
- case class Cat(name: String, color: String, favoriteFood:String){
- def eat(food:String) : String = {
- if (food==favoriteFood) "OMNOMNOM" else "Bleeeh"
- }
- def greet(person: Person): String = {
- person match {
- case Person("Noel") => "Hi"
- case Person(first) => s"Goodbye ${first}"
- case _ => "No"
- }
- }
- }
- object ChipShop {
- def serves(cat : Cat) : Boolean = {
- cat match {
- case Cat(_, _, "Chips") => true
- case _ => false
- }
- }
- }
- val oswald = Cat("Oswald", "Black", "Milk")
- val henderson = Cat("Henderson", "Ginger and White", "Chips")
- val quentin = Cat("Quentin", "Tabby and White", "Curry")
- val noel = Person("Noel")
- val joel = Person("Joel")
- trait Visitor {
- def id: String
- }
- //Algebraic Data Types, ANDing and ORing to make types
- case class Anon(id: String) extends Visitor
- case class User(id: String) extends Visitor
- trait Shape {
- def width: Int
- def height: Int
- def area : Double = this match {
- case Rectangle(width, height) => width*height
- case Circle(radius) => Math.pow(radius, 2) * Math.PI
- }
- }
- case class Rectangle(width: Int, height: Int) extends Shape {
- }
- case class Circle(radius : Int) extends Shape {
- def width : Int = {
- radius * 2
- }
- def height : Int = {
- radius * 2
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement