Repeating a List in Scala

Posted by Ralph on Stack Overflow See other posts from Stack Overflow or by Ralph
Published on 2010-03-22T11:54:01Z Indexed on 2010/03/22 12:21 UTC
Read the original article Hit count: 247

Filed under:

I am a Scala noob. I have decided to write a spider solitaire solver as a first exercise to learn the language and functional programming in general.

I would like to generate a randomly shuffled deck of cards containing 1, 2, or 4 suits. Here is what I came up with:

val numberOfSuits = 1
(List["clubs", "diamonds", "hearts", "spades"].take(numberOfSuits) * 4).take(4)

which should return

List["clubs", "clubs", "clubs", "clubs"]
List["clubs", "diamonds", "clubs", "diamonds"]
List["clubs", "diamonds", "hearts", "spades"]

depending on the value of numberOfSuits, except there is no List "multiply" operation that I can find. Did I miss it? Is there a better way to generate the complete deck before shuffling?

BTW, I plan on using an Enumeration for the suits, but it was easier to type my question with strings. I will take the List generated above and using a for comprehension, iterate over the suits and a similar List of card "ranks" to generate a complete deck.

© Stack Overflow or respective owner

Related posts about scala