Skip to content

Instantly share code, notes, and snippets.

@sebnozzi
Last active August 29, 2015 14:27
Show Gist options
  • Save sebnozzi/6a4b1717d4cd36b08a2c to your computer and use it in GitHub Desktop.
Save sebnozzi/6a4b1717d4cd36b08a2c to your computer and use it in GitHub Desktop.
import scala.util.Random
/**
* @author Sebastian Nozzi
*/
object RaffleApp extends App with HelperMethods {
lazy val numberCandidates: Set[Int] = {
val allNumbers = (1 to 17).toSet
val numbersThatWonSomething = Set[Int]()
allNumbers.diff(numbersThatWonSomething)
}
lazy val peopleCandidates: Set[String] = {
// Outcomment winners!
val peopleThatDidNotWinAnythingYet =
Seq(
"Person1",
"Person2",
"Person3",
//"Person4", // this guy already won something
"Person5")
assert(hasUniqueElements(peopleThatDidNotWinAnythingYet), "Should have unique names!")
peopleThatDidNotWinAnythingYet.toSet
}
val chosenOne = chooseOne(numberCandidates)
println(s"And the chosen one is... ${chosenOne}")
}
trait HelperMethods {
protected def hasUniqueElements[A](a: Seq[A]): Boolean = {
a.toSet.size == a.size
}
protected def chooseOne[A](candidates: Set[A]): A = {
Random.shuffle(candidates.toSeq).head
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment