Skip to content

Instantly share code, notes, and snippets.

View makiftutuncu's full-sized avatar
💭
🤘🏻

Mehmet Akif Tütüncü makiftutuncu

💭
🤘🏻
View GitHub Profile
@makiftutuncu
makiftutuncu / PeriodikBrokenTest.kt
Created February 1, 2024 20:55 — forked from upeter/PeriodikBrokenTest.kt
Test demonstrating that Periodik blocks Dispatcher Thread
package dev.akif.periodik
import dev.akif.periodik
import kotlinx.coroutines.*
import org.junit.jupiter.api.Timeout
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import kotlin.random.Random
import kotlin.reflect.KProperty
import kotlin.test.Test
@makiftutuncu
makiftutuncu / BarDBConfig.scala
Created July 16, 2022 07:53
Sharing Builder Code by Some Scala Tricks
case class BarDBConfig(connectionString: String) extends DBConfig
object BarDBConfig extends DBConfig.Builder[BarDBConfig]("bar")
@makiftutuncu
makiftutuncu / Backspaced.scala
Last active May 27, 2021 23:01
A Scala 3 exercise: simulates typing with backspace keys, removing designated backspace characters along with the character it's supposed to remove
import scala.annotation.tailrec
sealed trait Stack[A]:
def push(a: A): Stack[A]
def pop(): (Option[A], Stack[A])
def size: Int
def reverse(): Stack[A] =
@tailrec def go(s1: Stack[A], s2: Stack[A]): Stack[A] =
s1.pop() match
@makiftutuncu
makiftutuncu / JacksonExtensions.kt
Last active July 13, 2020 09:27
Extension methods for Jackson Json for typesafe Json handling in Kotlin
import com.fasterxml.jackson.databind.JsonNode
object JsonExtensions {
fun JsonNode.objectAt(key: String): JsonNode? = this.takeIf { it.isObject }?.get(key)?.takeIf { it.isObject }
fun JsonNode.arrayAt(key: String): JsonNode? = this.takeIf { it.isObject }?.get(key)?.takeIf { it.isArray }
fun JsonNode.booleanAt(key: String): Boolean? = this.takeIf { it.isObject && it.has(key) }?.get(key)?.takeIf { it.isBoolean }?.asBoolean()
fun JsonNode.intAt(key: String): Int? = this.takeIf { it.isObject && it.has(key) }?.get(key)?.takeIf { it.isIntegralNumber }?.asInt()
fun JsonNode.longAt(key: String): Long? = this.takeIf { it.isObject && it.has(key) }?.get(key)?.takeIf { it.isIntegralNumber }?.asLong()
fun JsonNode.doubleAt(key: String): Double? = this.takeIf { it.isObject && it.has(key) }?.get(key)?.takeIf { it.isFloatingPointNumber }?.asDouble()
@makiftutuncu
makiftutuncu / KotlinJson.kt
Created June 20, 2020 12:46
An almost complete JSON ADT in Kotlin
import java.math.BigDecimal
sealed class Json {
companion object {
fun `null`(): JNull = JNull
fun arr(vararg values: Json): JArray = JArray(*values)
fun obj(vararg pairs: Pair<String, Json>): JObject = JObject(*pairs)
@makiftutuncu
makiftutuncu / KotlinVarianceTrick.kt
Created May 26, 2020 12:34
Trick to Use Covariant (out) Type in Contravariant (in) Position in Kotlin
sealed class Option<out A>(open val value: A?) {
fun <B> fold(ifNone: () -> B, ifSome: (A) -> B): B =
when (this) {
is None -> ifNone()
is Some -> ifSome(this.value)
}
// This doesn't compile because `out` type A is in `in` position
fun getOrElseThatWontWork(default: () -> A): A =
when (this) {
@makiftutuncu
makiftutuncu / KotlinExtensions.scala
Created April 8, 2020 10:35
Kotlin Extensions in Scala
import scala.util.Try
implicit class KotlinLikeExtensions[A](a: => A) {
/**
* Produce a new value using current value
*/
def let[B](f: A => B): B = f(a)
/**
* Do something with current value and produce nothing
@makiftutuncu
makiftutuncu / InterfaceAbstractClass.java
Last active December 21, 2019 22:10
A cliché example of interface vs. abstract class in Java
// Behavior of talking
interface Talks {
void talk(String what);
}
// Behavior of making sound
interface MakesSound {
void makeSound(String what);
}
@makiftutuncu
makiftutuncu / rename_package.sh
Created August 29, 2019 13:23
Rename Package and Keep History
git filter-branch -f --prune-empty --tree-filter '
find . \
-name .git -prune -o \
-exec sh -c "file {} | grep -q text" \; \
-exec sed -i "" \
-e "s/X/Y/g" \
-e "s/com.x.y/com.github.makiftutuncu.trump/g" \
-e "s/x/y/g" \
{} \; \
&& mkdir -p src/main/scala/com/github/makiftutuncu/trump && mkdir -p src/test/scala/com/github/makiftutuncu/trump && tree src && mv src/main/scala/com/x/y/* src/main/scala/com/github/makiftutuncu/trump && mv src/test/scala/com/x/y/* src/test/scala/com/github/makiftutuncu/trump && tree src
@makiftutuncu
makiftutuncu / Either.java
Last active June 8, 2023 23:01
A Basic Implementation of Scala's Either in Java 8
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
public abstract class Either<L, R> {
public static final class Left<L, R> extends Either<L, R> {
public Left(L left) {
super(left, null);
}