Skip to content

Instantly share code, notes, and snippets.

@davegurnell
Created March 22, 2021 16:53
Show Gist options
  • Select an option

  • Save davegurnell/621d60924fb8b7180be80d22d3387eb9 to your computer and use it in GitHub Desktop.

Select an option

Save davegurnell/621d60924fb8b7180be80d22d3387eb9 to your computer and use it in GitHub Desktop.

Revisions

  1. davegurnell created this gist Mar 22, 2021.
    32 changes: 32 additions & 0 deletions ColorAndShape.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    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
    }
    }
    }