Skip to content

Instantly share code, notes, and snippets.

View cassiozen's full-sized avatar
:atom:

Cassio Zen cassiozen

:atom:
View GitHub Profile
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")
function weatherReport(location) {
// Make an Ajax request to fetch the weather...
return [72, "Mostly Sunny"];
}
const [temp, forecast] = weatherReport("Berkeley, CA");
val coordinates = arrayOf(5, 10, 15)
val (x, y, z) = coordinates
const coordinates = [5, 10, 15];
const [x, y, z] = coordinates;
// 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 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
const colors = {
"red": 0xff0000,
"green": 0x00ff00,
"blue": 0x0000ff,
"cyan": 0x00ffff,
"magenta": 0xff00ff,
"yellow": 0xffff00
};
colors.hasOwnProperty("yellow"); // true
colors.yellow; // 0xffff00
val houses = mutableListOf("Stark", "Lannister", "Tyrell", "Arryn", "Targaryen", "Martell", "Baratheon")
houses[2] // "Tyrell"
houses.add("Martell")
houses.size //7
const houses = [ "Stark", "Lannister", "Tyrell", "Arryn", "Targaryen", "Baratheon" ];
houses[2]; // "Tyrell"
houses.push("Martell");
houses.length; //7
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")
}