Skip to content

Instantly share code, notes, and snippets.

@sorokod
sorokod / Result.kt
Created March 31, 2018 22:38
Result and OptionalResult sealed classes
package result
import result.Result.Err
import result.Result.Ok
sealed class Result<out T> {
data class Ok<out T>(val value: T) : Result<T>()
data class Err(val exception: Exception = RuntimeException(), val msg: String = "") : Result<Nothing>()
}
@sorokod
sorokod / GaleShapley.kt
Created May 27, 2018 19:20
Gale-Shapley (stable marriage) algorithm for Rosetta Code
data class Person(val name: String) {
val preferences = mutableListOf<Person>()
var matchedTo: Person? = null
fun trySwap(p: Person) {
if (prefers(p)) {
matchedTo?.matchedTo = null
matchedTo = p
p.matchedTo = this
@sorokod
sorokod / KotlinFavorite.kt
Last active January 26, 2022 14:40
Kotlin - favorite
/**
* List literals and List.get extensions
*/
object L {
// L[1,2,3,4]
inline operator fun <T> get(vararg a:T): List<T> = listOf(*a)
}
// aList[0..3]
@sorokod
sorokod / Pi_coPrime.kt
Last active December 29, 2018 11:21
Approximate Pi from randomly generated integers
import com.google.common.math.IntMath
import java.lang.Math.sqrt
import kotlin.random.Random.Default.nextInt
import kotlin.system.measureTimeMillis
/**
* Approximate Pi from randomly generated integers given that Pr( coPrime(n,m) ) = 6 / ( pi^2 ).
* Guava's implementation of GCD is about twice as fast as the "naive" one.
*/
@sorokod
sorokod / Piskie.kt
Last active October 23, 2019 19:30
A quick way of displaying pixels (with optional animation) in a JFrame
import java.awt.Color.BLACK
import java.awt.Dimension
import java.awt.Graphics
import java.awt.Toolkit
import java.awt.image.BufferedImage
import java.awt.image.BufferedImage.TYPE_INT_ARGB
import java.awt.image.BufferedImage.TYPE_INT_RGB
import javax.swing.JFrame
import javax.swing.JPanel
class MyClass {
fun test() {
val str: String = "..."
val result = str.xxx {
print(this) // Receiver
print(it) // Argument
42 // Block return value
}
}
@sorokod
sorokod / ECDH.kt
Last active August 1, 2024 20:12
Generate a shared secret key between two parties using ECDH
package crypto
import java.security.*
import java.security.spec.X509EncodedKeySpec
import java.util.*
import javax.crypto.KeyAgreement
/**
* Generate a shared secret key between two parties using ECDH
*
class TimingLogger(
private val step: Long,
private val timeUnit: TimeUnit,
private val template: String,
private val counterScaler: Long = 1,
private val log: (String) -> Unit
) {
private var tickCounter: Long = 0L
private var timeMark = System.nanoTime()
/**
* A type safe wrapper for comparable IDs
**/
import java.util.UUID
abstract class RefId<T : Comparable<T>> : Comparable<RefId<T>> {
abstract val value: T
override fun compareTo(other: RefId<T>) = value.compareTo(other.value)
@sorokod
sorokod / Abusing Result.mapCatching.kt
Last active January 8, 2022 17:13
Abusing Result.mapCatching() to implement a chain of transformations validations
/**
* Abusing Result.mapCatching() to implement a chain
* of transformations / validations
*/
/** A convenience thing **/
inline fun <reified T> fail(msg: String): T = throw IllegalArgumentException(msg)
/** **/