July 2018
Intermediate to advanced
400 pages
12h 14m
English
In NyetHack, you create a mutable set of unique patron names by feeding the elements from a list into it, one by one. You can also convert a list to a set, or vice versa, using the toSet and toList functions (or their mutable cousins: toMutableSet and toMutableList). A common trick is to call toSet to drop the non-unique elements in a list. (Try these experiments in the REPL.)
Listing 10.27 Converting a list to a set (REPL)
listOf("Eli Baggins", "Eli Baggins", "Eli Ironfoot").toSet()
[Eli Baggins, Eli Ironfoot]
If you want quick index-based access after converting a list to a set to remove duplicates, you can convert the set back to a list:
Listing 10.28 Converting a set back to a list (REPL) ...
Read now
Unlock full access