Skip to content

Instantly share code, notes, and snippets.

@lawrenceching
Last active July 25, 2019 16:51
Show Gist options
  • Save lawrenceching/024a2e2c1faae3b917efd2a8b4b7a40d to your computer and use it in GitHub Desktop.
Save lawrenceching/024a2e2c1faae3b917efd2a8b4b7a40d to your computer and use it in GitHub Desktop.
hello-kotlin
package com.example
import java.util.*
import kotlinx.coroutines.*
import kotlin.system.measureTimeMillis
val title = "Kotlin Demo"
var content = "A 20-mins Kotlin sharing"
var optional: String? = null
// String and template
// language=html
val html = """
<html>
<head>
<title>$title</title>
</head>
<body>
$content
</body>
</html>
""".trimIndent()
// For Loop
fun forLoop() {
for(i in 0..9) {
println(i)
}
for(i in 0..10 step 2) {
println(i)
}
for(i in 10 downTo 0 step 2) {
println(i)
}
for(i in listOf("A", "B", "C")) {
println(i)
}
listOf("A", "B", "C").forEach {
println(it)
}
}
fun switchExpr() {
val id = 1;
when (id) {
1 -> println(id)
2, 3 -> println(id)
in 4..5 -> println(id)
!in 4..5 -> println(id)
is Int -> println(id)
else -> println(id)
}
fun Int.isEven(): Boolean {
return true
}
fun isPrime(x: Int): Boolean {
return true
}
when {
id.isEven() -> println(id)
isPrime(id) -> println(id)
}
}
fun collection() {
val list = listOf<Int>(1, 2, 3)
val set = setOf<String>("A", "B", "C")
var map = mapOf<Int, String>(
1 to "A",
2 to "B"
)
val mutalbeList = arrayListOf(1)
val mutableSet = hashSetOf("ABC")
val mutableMap = hashMapOf("OOO" to 123)
list.map { it * 10 }
.filter(
fun(number: Int): Boolean {
return number > 1
}
)
}
fun functions() {
fun say(msg: String) {
println("Hello, $msg")
}
say("Lawrence")
say("Mike")
say("Jay")
data class QuoteRequest(
val id: String,
val currency: String,
val product: String,
val reasonCode: String = "N/A")
val quoteRequest = QuoteRequest(
id = UUID.randomUUID().toString(),
product = "SPOT",
currency = "EURUSD"
)
fun QuoteRequest.toJSON(): String {
return """
{
"id": "${this.id}",
"currency": "${this.currency}",
"product": "${this.product}",
"reasonCode": "${this.reasonCode}",
}
""".trimIndent()
}
}
fun classes() {
class User {
private val name: String
private var age: Int
constructor(name: String, age: Int) {
this.name = name;
this.age = age
}
fun getName(): String {
return this.name
}
fun getAge(): Int {
return this.age
}
fun setAge(age: Int) {
this.age = age
}
}
class Instance {
var instanceId: String
get() {
return "ID_15123123"
}
set(value) {
instanceId = value
}
}
class Service {
fun start() {}
fun stop() {}
}
// class DemoTest {
//
// lateinit var service: Service
//
// @Before
// fun setUp() {
// service = Service()
// service.start()
// }
//
// @After
// fun tearDown() {
// service.stop()
// }
//
// }
}
fun coroutines() {
GlobalScope.launch { // launch a new coroutine in background and continue
delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
println("World!") // print after delay
}
println("Hello,") // main thread continues while coroutine is delayed
Thread.sleep(2000L)
runBlocking {
fun doSomethingSync(): String {
return "Something"
}
val something = async {
delay(3000L)
doSomethingSync()
}
val otherThing = async {
delay(3000)
listOf(1,2,3).findLast { it >= 2 }
}
val duration = measureTimeMillis {
println("${something.await()} is ${otherThing.await()}")
}
println("Duration: $duration")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment