Skip to content

Instantly share code, notes, and snippets.

@cassiozen
Created November 29, 2018 00:18
Show Gist options
  • Save cassiozen/d9417d1078a420c8a6555623b55896c0 to your computer and use it in GitHub Desktop.
Save cassiozen/d9417d1078a420c8a6555623b55896c0 to your computer and use it in GitHub Desktop.
// mapOf is the read-only version of mutableMapof
val colors = mapOf(
"red" to 0xff0000,
"green" to 0x00ff00,
"blue" to 0x0000ff
)
val updatedColors = colors.plus("teal" to 0x008080) // doesn't change the original - it returns a new map
// listOf is the read-only version of mutableListof
val houses = listOf("Stark", "Lannister", "Tyrell", "Arryn", "Targaryen", "Martell", "Baratheon")
// Methods that return a new list instead of modifying it are still available:
var updatedHouses = houses.take(3).map {it.toUpperCase()} //["STARK", "LANNISTER", "TYRELL"]
// Adding new items requires copying the whole original one and making sure the new copy is also immutable
var updatedHouses = houses.toMutableList().apply{ add("Martell") }.toList()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment