Skip to content

Instantly share code, notes, and snippets.

View cy6erGn0m's full-sized avatar
🦎

Sergey Mashkov cy6erGn0m

🦎
View GitHub Profile
@cy6erGn0m
cy6erGn0m / ktor-route-attributes
Last active October 23, 2018 08:30
Assigning call attributes based or route with default
private val XFrameOptionsAttribute = AttributeKey<XFrameOptions>("X-Frame-Options")
sealed class XFrameOptions {
object Deny : XFrameOptions() {
override fun toString() = "deny"
}
object SameOrigin : XFrameOptions() {
override fun toString() = "sameorigin"
}
class AllowFrom(val url: String) : XFrameOptions() {
@cy6erGn0m
cy6erGn0m / MyJavaBlockingQueue.kt
Created November 21, 2018 14:20
Simplified async to blocking adapter
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import java.util.concurrent.locks.*
import kotlin.coroutines.*
private val MyEventLoop: CoroutineDispatcher = TODO()
/**
* This is simplified blocking adapter example.
* NEVER use in production as it is just clarification, not actual implementation
@cy6erGn0m
cy6erGn0m / Example.kt
Created September 15, 2021 16:32
Using Java LambdaMetafactory to convert Kotlin property to lambda without reflections and faster
// Please note that you don't even need kotlin-reflect.jar
class C {
val f: String
get() = "ok"
}
fun usage() {
val fn = makeFunction(C::f)
println(fn(C()))
@cy6erGn0m
cy6erGn0m / MergeSort.kt
Created July 31, 2022 14:03
Simple non-recursive merge sort implementation in Kotlin
fun <T : Comparable<T>> mergeSort(input: Array<T>) {
mergeSort(input, naturalOrder())
}
fun <T> mergeSort(input: Array<T>, comparator: Comparator<T>) {
if (input.size < 2) return
val tempSpace = input.copyOf()
for (windowSize in generateSequence(1) { it * 2 }.takeWhile { it < input.size }) {
for (segment in 0 until input.size / windowSize step 2) {
@cy6erGn0m
cy6erGn0m / merge-maps.kt
Created May 20, 2015 14:41
Merge two maps with custom reduce function for Kotlin
private fun <K, V> Map<K, V>.mergeReduce(other: Map<K, V>, reduce: (V, V) -> V = { a, b -> b }): Map<K, V> {
val result = LinkedHashMap<K, V>(this.size() + other.size())
result.putAll(this)
other.forEach { e ->
val existing = result[e.key]
if (existing == null) {
result[e.key] = e.value
}
else {
@cy6erGn0m
cy6erGn0m / freeport.kt
Created January 18, 2016 16:34
Kotlin get free port
fun findFreePort() = ServerSocket(0).use { it.localPort }
@cy6erGn0m
cy6erGn0m / kotlin-websocket-example.kt
Last active June 20, 2023 15:28
This example demonstrates how to use WebSocket including reconnect when needed and error handling
import org.w3c.dom.MessageEvent
import org.w3c.dom.WebSocket
import kotlin.browser.window
class KWebSocketImpl(val url : String, val reconnectDelayMillis: Int, val listener : (dynamic) -> Unit) {
private var currentSocket : WebSocket? = null
private val queue = arrayListOf<String>()
private var closed = false
init {
@cy6erGn0m
cy6erGn0m / Main.kt
Last active February 16, 2024 05:29
kotlin-native-libssh
import kotlinx.cinterop.*
import org.ssh.*
fun main(): Unit = memScoped {
val session = ssh_new() ?: return
val port = alloc<IntVar>()
port.value = 22
val verbosity = alloc<UIntVar>()
verbosity.value = SSH_LOG_PROTOCOL
@cy6erGn0m
cy6erGn0m / kotlin-ehcache.kt
Created February 5, 2016 15:59
Kotlin ehcache example
val cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.with(CacheManagerPersistenceConfiguration(storagePath))
.withCache("kweetsCache",
CacheConfigurationBuilder.newCacheConfigurationBuilder<Int, Kweet>()
.withResourcePools(ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(1000, EntryUnit.ENTRIES)
.offheap(10, MemoryUnit.MB)
.disk(100, MemoryUnit.MB, true)
)
.buildConfig(Int::class.javaObjectType, Kweet::class.java))