Tavern.kt could be improved by using some of the functional programming features you learned about in this chapter.
Consider the forEach loop that you use to generate the unique patron names:
val uniquePatrons = mutableSetOf<String>() fun main(args: Array<String>) { ... (0..9).forEach { val first = patronList.random() val last = lastName.random() val name = "$first $last" uniquePatrons += name } ... }
The loop mutates the state of the uniquePatrons set every iteration. This works – but it is possible to do better using a functional programming approach. You might express the uniquePatrons set like this instead:
val uniquePatrons: Set<String> = generateSequence { val first = patronList.random() val last = lastName.random() "$first $last" }.take(10).toSet()
This is an improvement over the old version, because the mutable set is no longer required and you can make the collection read-only.
Notice that the number of uniquePatrons currently varies, depending on chance. For your first challenge, use the generateSequence function to generate exactly nine unique patron names. (Look back at the example in this chapter that generated exactly 1,000 prime numbers for a hint.)
For a second challenge, using what you learned in this section, upgrade the code in Tavern.kt that populates the patron gold map with initial values:
fun main(args: Array<String>) { ... uniquePatrons.forEach { patronGold[it] = 6.0 } ... }
The new version should perform the setup for the patronGold set where the variable is defined, rather than within the main function.