Skip to content

Instantly share code, notes, and snippets.

@rhyskeepence
Created October 30, 2012 22:50
Show Gist options
  • Save rhyskeepence/3983616 to your computer and use it in GitHub Desktop.
Save rhyskeepence/3983616 to your computer and use it in GitHub Desktop.
Scala stereotype (solution for https://github.com/raymanoz/function-this-up)
package example
object Stereotypes {
val stereotypes = Seq(australia, newzealand, preston, liverpool, manchester)
def find(name: String): Either[String, Stereotype] = {
stereotypes
.find(stereotype => stereotype.names.exists(name.equalsIgnoreCase))
.toRight("Invalid Stereotype [%s]. Must be one of [%s]".format(name, allPossibleStereotypes))
}
def allPossibleStereotypes = stereotypes.flatMap(s => s.names).sorted.mkString(", ")
}
sealed abstract case class Stereotype(description: String, aliases: String*) {
val names = toString :: description :: aliases.toList
}
case object australia extends Stereotype("a person from australia", "aussie", "legend")
case object newzealand extends Stereotype("a person from new zealand", "kiwi")
case object preston extends Stereotype("a person from preston", "scally")
case object liverpool extends Stereotype("a person from liverpool", "scouser", "thief")
case object manchester extends Stereotype("a person from manchester", "manc", "mancunian", "fighter")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment