This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun weatherReport(location) { | |
// Make an Ajax request to fetch the weather... | |
return Pair(72, "Mostly Sunny") // Pair is a standard class in Kotlin that represents a generic pair of two values | |
} | |
val (temp, forecast) = weatherReport("Berkeley, CA") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function weatherReport(location) { | |
// Make an Ajax request to fetch the weather... | |
return [72, "Mostly Sunny"]; | |
} | |
const [temp, forecast] = weatherReport("Berkeley, CA"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val coordinates = arrayOf(5, 10, 15) | |
val (x, y, z) = coordinates |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const coordinates = [5, 10, 15]; | |
const [x, y, z] = coordinates; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val colors = mutableMapOf( | |
"red" to 0xff0000, | |
"green" to 0x00ff00, | |
"blue" to 0x0000ff, | |
"cyan" to 0x00ffff, | |
"magenta" to 0xff00ff, | |
"yellow" to 0xffff00 | |
) | |
colors.contains("yellow") // true | |
colors.get("yellow") // 0xffff00 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const colors = { | |
"red": 0xff0000, | |
"green": 0x00ff00, | |
"blue": 0x0000ff, | |
"cyan": 0x00ffff, | |
"magenta": 0xff00ff, | |
"yellow": 0xffff00 | |
}; | |
colors.hasOwnProperty("yellow"); // true | |
colors.yellow; // 0xffff00 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val houses = mutableListOf("Stark", "Lannister", "Tyrell", "Arryn", "Targaryen", "Martell", "Baratheon") | |
houses[2] // "Tyrell" | |
houses.add("Martell") | |
houses.size //7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const houses = [ "Stark", "Lannister", "Tyrell", "Arryn", "Targaryen", "Baratheon" ]; | |
houses[2]; // "Tyrell" | |
houses.push("Martell"); | |
houses.length; //7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (i in 1..10) { | |
print(i) | |
} | |
// 1 2 3 4 5 6 7 8 9 10 | |
val places = listOf("New York", "Paris", "Rio") | |
for (place in places) { | |
println("I Love $place") | |
} |