Skip to content

Instantly share code, notes, and snippets.

View cy6erGn0m's full-sized avatar
🦎

Sergey Mashkov cy6erGn0m

🦎
View GitHub Profile
@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 / 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 / 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 / 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 / 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() {
sealed class XFrameOptions {
object Deny : XFrameOptions() {
override fun toString() = "deny"
}
object SameOrigin : XFrameOptions() {
override fun toString() = "sameorigin"
}
class AllowFrom(val url: String) : XFrameOptions() {
override fun toString(): String = "allow-from $url"
}
@cy6erGn0m
cy6erGn0m / JettyClientTest.kt
Created July 28, 2016 15:10
Jetty HTTP client issue testcase
package org.jetbrains.ktor.client
import org.eclipse.jetty.http2.client.*
import org.eclipse.jetty.http2.client.http.*
import org.eclipse.jetty.http2.server.*
import org.eclipse.jetty.server.*
import org.eclipse.jetty.servlet.*
import org.eclipse.jetty.util.thread.*
import org.junit.*
import java.util.concurrent.*
@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))
@cy6erGn0m
cy6erGn0m / freeport.kt
Created January 18, 2016 16:34
Kotlin get free port
fun findFreePort() = ServerSocket(0).use { it.localPort }
@cy6erGn0m
cy6erGn0m / build.gradle
Last active December 23, 2015 10:23
Configuring testing library in kotlin
dependencies {
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion"
}