Skip to content

Instantly share code, notes, and snippets.

@cesartl
cesartl / HttpDigestInterceptor.kt
Last active June 3, 2023 22:29
Feign Http Digest Authorization
import feign.*
import feign.codec.ErrorDecoder
import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.lang.RandomStringUtils
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
@cesartl
cesartl / fibonacciSlow.kt
Last active May 17, 2020 13:47
Basic Fibonacci Implementation
fun fibonacciSlow(n: BigInteger) = when {
n <= BigInteger.ONE -> n
else -> fibonacciSlow(n - BigInteger.ONE) + fibonacciSlow(n - BigInteger.TWO)
}
public interface Semigroup<A> {
public abstract fun A.combine(b: A): A
}
interface HasBinaryOp<T>{
T combine(T other);
T empty();
}
public class Foo extends Semigroup<Foo>{
@Override
public Foo combine(Foo other){
return ...
}
class FooSemigroup : Semigroup<Foo>{
override fun Foo.combine(f: Foo): Foo{
return ...
}
}
public interface Monoid<A> : Semigroup<A>
public abstract fun empty(): A
}
/**
* Exponentiation by squaring
* https://en.wikipedia.org/wiki/Exponentiation_by_squaring
*/
fun <T> Monoid<T>.combineTimes(m: T, k: BigInteger): T {
if (k == BigInteger.ZERO) {
return empty()
}
if (k == BigInteger.ONE) {
return m
data class Matrix22(
val x00: BigInteger,
val x01: BigInteger,
val x10: BigInteger,
val x11: BigInteger
) {
companion object {
val identity = Matrix22(BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE)
}
}
interface Matrix22Monoid : Monoid<Matrix22> {
override fun empty(): Matrix22 = Matrix22.identity
override fun Matrix22.combine(b: Matrix22): Matrix22 {
val (l00, l01, l10, l11) = this
val (r00, r01, r10, r11) = b
return Matrix22(
l00 * r00 + l01 * r10,
l00 * r01 + l01 * r11,
l10 * r00 + l11 * r10,
l10 * r01 + l11 * r11