Skip to content

Instantly share code, notes, and snippets.

@davegurnell
Created March 22, 2021 16:53
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 davegurnell/621d60924fb8b7180be80d22d3387eb9 to your computer and use it in GitHub Desktop.
Save davegurnell/621d60924fb8b7180be80d22d3387eb9 to your computer and use it in GitHub Desktop.
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