Created
March 22, 2021 16:53
-
-
Save davegurnell/621d60924fb8b7180be80d22d3387eb9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Color(red: Double, green: Double, blue: Double) { | |
def isLight: Boolean = | |
(red + green + blue) / 3.0 >= 0.5 | |
def toGreyscale: Color = { | |
val avg = (red + green + blue) / 3.0 | |
Color(avg, avg, avg) | |
} | |
} | |
sealed trait Shape { | |
def area: Double | |
} | |
case class Circle(radius: Double, color: Color) extends Shape { | |
def area: Double = | |
radius * radius * math.Pi | |
} | |
case class Rect(width: Double, height: Double, color: Color) extends Shape { | |
def area: Double = | |
width * height | |
} | |
object otherLibrary { | |
def perimeterOf(shape: Shape): Double = { | |
shape match { | |
case Circle(r, c) => 2 * math.Pi * r | |
case Rect(w, h, c) => 2 * w + 1 * h | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment