Skip to content

Instantly share code, notes, and snippets.

import org.springframework.scheduling.support.CronExpression
import java.time.LocalDateTime
import java.time.LocalDateTime.now
/**
* Let Spring generate a sequence of dates according top the provided cron string.
* See: https://spring.io/blog/2020/11/10/new-in-spring-5-3-improved-cron-expressions
*/
fun genTimes(cronStr: String): Sequence<LocalDateTime> =
CronExpression.parse(cronStr).let { exp ->
/**
* Not sure who the original author is
**/
sealed class Either<out L, out R> {
data class Left<out T>(val value: T) : Either<T, Nothing>()
data class Right<out T>(val value: T) : Either<Nothing, T>()
}
inline fun <L, R, T> Either<L, R>.fold(left: (L) -> T, right: (R) -> T): T =
@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)
/** **/
/**
* 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)
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()
@sorokod
sorokod / ECDH.kt
Last active April 26, 2023 13:14
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 MyClass {
fun test() {
val str: String = "..."
val result = str.xxx {
print(this) // Receiver
print(it) // Argument
42 // Block return value
}
}
@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
@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 / 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]