Skip to content

Instantly share code, notes, and snippets.

@ncomet
ncomet / src.kt
Created April 12, 2019 05:51
sealed class
sealed class BookingOutcome
sealed class BookingOutcome
class RoomBooked(key: Key, bookingRange: ClosedRange<Instant>) : BookingOutcome()
object RoomNotAvailable : BookingOutcome()
object HotelClosed : BookingOutcome()
interface BookingService {
fun bookRoom(roomId: RoomID): BookingOutcome
}
val outcome = bookingService.bookRoom(RoomID("roomIwant"))
when (outcome) {
}
@ncomet
ncomet / Anagrams.kt
Last active April 24, 2021 16:16
Anagrams in Kotlin
// we create a sequence that generates prime numbers
fun primes(): Sequence<Long> {
var i = 2L
return generateSequence { i++ }
.filter { n -> (2L until n).none { n % it == 0L } }
}
// we map chars to a unique prime
val charToPrime = ('a'..'z').zip(primes().take(26).toList()).toMap()
@ncomet
ncomet / src.kt
Created September 16, 2020 08:43
"""{
"cat": "meow",
"dog": "woof"
}""".trimIndent()
@ncomet
ncomet / src.kt
Created September 16, 2020 08:52
"""
|{
| "cat": "meow",
| "dog": "woof"
|}
""".trimMargin()
@ncomet
ncomet / src.kt
Created September 16, 2020 08:54
val obj = obj {
"key" to 3.4
"anotherKey" to arr["test", "test2", 1, 2.433, true]
"nullValue" to null
"emptyObject" to obj { }
"emptyArray" to arr
"custom" to ZonedDateTime.now()
}
println(obj) // 1
{"key":3.4,"anotherKey":["test","test2",1,2.433,true],"nullValue":null,"emptyObject":{},"emptyArray":[],"custom":"2020-07-15T10:59:19.042965+02:00[Europe/Paris]"}
{
"key": 3.4,
"anotherKey": [
"test",
"test2",
1,
2.433,
true
],
"nullValue": null,