Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
🤘🏻

Mehmet Akif Tütüncü makiftutuncu

💭
🤘🏻
View GitHub Profile
@makiftutuncu
makiftutuncu / BarDBConfig.scala
Created July 16, 2022 07:53
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 / 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
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 July 13, 2020 09:27
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 June 20, 2020 12:46
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 12:34
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 April 8, 2020 10:35
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 December 21, 2019 22:10
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 August 29, 2019 13:23
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
@makiftutuncu
makiftutuncu / Either.java
Last active March 6, 2022 18:43
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);
}
View PrivacyPolicyForTextInstead.html
<!DOCTYPE html><html><body><h2>Privacy Policy</h2><p>Mehmet Akif Tütüncü built the Text Instead? app as an open source app. This SERVICE is provided by Mehmet Akif Tütüncü at no cost and is intended for use as is.</p><p>This page is used to inform website visitors regarding my policies with the collection, use, and disclosure of Personal Information if anyone decided to use my Service.</p><p>If you choose to use my Service, then you agree to the collection and use of information in relation to this policy. The Personal Information that I collect are used for providing and improving the Service.I will not use or share your information with anyone except as described in this Privacy Policy.</p> <p>The terms used in this Privacy Policy have the same meanings as in our Terms and Conditions, which is accessible at Text Instead?, unless otherwise defined in this Privacy Policy.</p><p><strong>Information Collection and Use</strong></p><p>For a better experience, while using our Service, I may require you to provide