Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
🤘🏻

Mehmet Akif Tütüncü makiftutuncu

💭
🤘🏻
View GitHub Profile
@makiftutuncu
makiftutuncu / BarDBConfig.scala
Created Jul 16, 2022
Sharing Builder Code by Some Scala Tricks
View BarDBConfig.scala
case class BarDBConfig(connectionString: String) extends DBConfig
object BarDBConfig extends DBConfig.Builder[BarDBConfig]("bar")
@makiftutuncu
makiftutuncu / Either.java
Last active Mar 6, 2022
A Basic Implementation of Scala's Either in Java 8
View Either.java
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);
}
@makiftutuncu
makiftutuncu / Backspaced.scala
Last active May 27, 2021
A Scala 3 exercise: simulates typing with backspace keys, removing designated backspace characters along with the character it's supposed to remove
View Backspaced.scala
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 Jul 13, 2020
Extension methods for Jackson Json for typesafe Json handling in Kotlin
View JacksonExtensions.kt
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 Jun 20, 2020
An almost complete JSON ADT in Kotlin
View KotlinJson.kt
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
Trick to Use Covariant (out) Type in Contravariant (in) Position in Kotlin
View KotlinVarianceTrick.kt
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 Apr 8, 2020
Kotlin Extensions in Scala
View KotlinExtensions.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 Dec 21, 2019
A cliché example of interface vs. abstract class in Java
View InterfaceAbstractClass.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 Aug 29, 2019
Rename Package and Keep History
View rename_package.sh
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
View keybase.md

Keybase proof

I hereby claim:

  • I am makiftutuncu on github.
  • I am makiftutuncu (https://keybase.io/makiftutuncu) on keybase.
  • I have a public key whose fingerprint is E83C 323C 34DA DD70 97FB AB7A 3F26 DC07 5227 C356

To claim this, I am signing this object: