Skip to content

Instantly share code, notes, and snippets.

@sorokod
sorokod / ECharts-Barchart-with-Timeline.js
Last active September 22, 2025 18:24
Apache ECharts - A barchart with timeline
var chart = echarts.init(document.getElementById("display-container"));
const datasetSource = [
["product", "2015", "2016", "2017"],
["Matcha Latte", 43.3, 85.8, 93.7],
["Milk Tea", 83.1, 73.4, 55.1],
["Cheese Cocoa", 86.4, 65.2, 82.5],
["Walnut Brownie", 72.4, 53.9, 39.1]
];
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 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 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.
*/