July 2018
Intermediate to advanced
400 pages
12h 14m
English
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 ...Read now
Unlock full access