So the Scala solution for this is: http://ideone.com/Vussjh
object Cards {
sealed trait Suit
case object Spades extends Suit
case object Clubs extends Suit
case object Hearts extends Suit
case object Diamonds extends Suit
sealed trait Rank
trait FaceCard extends Rank
case object Jack extends FaceCard
case object Queen extends FaceCard
case object King extends FaceCard
case object Ace extends FaceCard
case class ValueCard(n: Int) extends Rank
case class Card(rank: Rank, suit: Suit)
type Deck = IndexedSeq[Card]
def numberToRank(n: Int): Rank = n match {
case 1 => Ace
case x if x <= 10 => ValueCard(x)
case 11 => Jack
case 12 => Queen
case 13 => King
case 14 => Ace
case _ => throw new IllegalArgumentException
}
def numberToSuit(n: Int): Suit = n match {
case 0 => Spades
case 1 => Clubs
case 2 => Hearts
case 3 => Diamonds
}
def apply(): Deck = for(rank <- 1 to 13; suit <- 0 to 3) yield Card(numberToRank(rank), numberToSuit(suit))
}
Now I have: normal operations bound to Deck (head, take, drop etc) and the ability to pattern match on cards. Also strongly typed. This took about 5 minutes top to write.edit: I'd say the 'numberToRank' parts are probably code smells, but given that its Christmas Eve, I really can't be bothered to think of a better solution right now. Implementing ordering etc can be done on the traits very easily too, but that is game specific (for example are aces high or low, is a King ranked higher than a Jack etc).