Skip to content

Instantly share code, notes, and snippets.

@1gravity
Last active July 7, 2023 17:02
Show Gist options
  • Save 1gravity/141f91cd1eba0cfe0aa0836a02a1bb6e to your computer and use it in GitHub Desktop.
Save 1gravity/141f91cd1eba0cfe0aa0836a02a1bb6e to your computer and use it in GitHub Desktop.
Kotlin 1.9
package com.vivint
enum class Color(val code: Long) {
RED(0xff0000L),
GREEN(0x00ff00L),
BLUE(0x0000ffL)
}
fun main() {
// 1. Stable replacement of the enum class values function
Color.values().forEach { println(it.code) } // This is not stable
Color.entries.forEach { println(it.code) } // This is stable
// 2. Stable ..< operator for open-ended ranges
for (number in 2 until 10) {
print("$number ")
}
for (number in 2..<10) { // This is stable and replaces 2 until 10
print("$number ")
}
// 3. New common function to get regex capture group by name
val regex = """\b(?<city>[A-Za-z\s]+),\s(?<state>[A-Z]{2}):\s(?<areaCode>[0-9]{3})\b""".toRegex()
val input = "Coordinates: Austin, TX: 123"
val match = regex.find(input) ?: return
println(match.groups["city"]?.value) // -> Austin
println(match.groups["state"]?.value) // -> TX
println(match.groups["areaCode"]?.value) // -> 123
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment