src/main/scala/allergies.scala

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
object Allergen extends Enumeration {
  val Eggs         = Value(1)
  val Peanuts      = Value(2)
  val Shellfish    = Value(4)
  val Strawberries = Value(8)
  val Tomatoes     = Value(16)
  val Chocolate    = Value(32)
  val Pollen       = Value(64)
  val Cats         = Value(128)
}

object Allergies {
  def apply() = this

  def isAllergicTo(allergen: Allergen.Value, score: Int) = (score & allergen.id) > 0

  def allergies(score: Int): List[Allergen.Value] =
    Allergen.values.filter(isAllergicTo(_, score)).toList
}

@abo64 thinks this looks great

Comments

although I prefer sealed traits over Enumerations.

abo64 commented 20 May 2016 at 10:03 UTC

You're not logged in right now. Please login via GitHub to comment