This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Abusing Result.mapCatching() to implement a chain | |
* of transformations / validations | |
*/ | |
/** A convenience thing **/ | |
inline fun <reified T> fail(msg: String): T = throw IllegalArgumentException(msg) | |
/** **/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass { | |
fun test() { | |
val str: String = "..." | |
val result = str.xxx { | |
print(this) // Receiver | |
print(it) // Argument | |
42 // Block return value | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
*/ |
NewerOlder