Skip to content

Instantly share code, notes, and snippets.

@tanmatra
tanmatra / Player.kt
Created May 13, 2021 16:39
IDEA with Kotlin plugin freeze
package example
class DataChannel {
fun registerObserver(dataChannelObserver: DataChannelObserver?) {}
}
interface DataChannelObserver {
fun onStateChange()
}
import java.lang.reflect.InvocationHandler
import java.lang.reflect.Method
import java.lang.reflect.Proxy
import java.util.Timer
import kotlin.concurrent.timerTask
import kotlin.coroutines.Continuation
import kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlinx.coroutines.CancellationException
@tanmatra
tanmatra / algebraic_effects.kt
Created October 9, 2019 12:16
Simple "algebraic effects" implementation on Kotlin
/*
* https://overreacted.io/algebraic-effects-for-the-rest-of-us/
*/
private val effectsStack = ThreadLocal<EffectsFrame>()
fun <T> perform(effectKey: Any): T {
var frame = effectsStack.get()
while (frame != null) {
@Suppress("UNCHECKED_CAST")
@tanmatra
tanmatra / json.kt
Created September 18, 2019 12:47
Kotlin JSON builder
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonArray
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.google.gson.JsonParser
@DslMarker
annotation class JsonDSL
@tanmatra
tanmatra / README.md
Last active July 7, 2019 20:18
Kotlin hash code builder with inline class
@tanmatra
tanmatra / TreeTableViewTestApp.java
Created October 16, 2017 15:47
JavaFX TreeTableView bug in Java 9
import java.util.Random;
import javafx.application.Application;
import javafx.beans.property.ReadOnlyIntegerWrapper;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
import javafx.scene.layout.BorderPane;
@tanmatra
tanmatra / Example.kt
Last active October 2, 2022 08:57
Kotlin hashCode builder with inline functions
class Example(val time: Long,
val short: Short = -111,
val eventId: Int?,
val data: ByteArray?,
val extra: Any = Unit,
val extra2: LongArray? = null)
{
override fun hashCode() = initHash()
.addHash(time)
.addHash(short)
@tanmatra
tanmatra / Caller.java
Created October 3, 2017 18:09
Kotlin SAM conversions
import java.util.Date;
import java.util.function.Consumer;
public interface Caller
{
void call(String id, Consumer<Integer> integerConsumer, Consumer<String> stringConsumer, Consumer<Date> dateConsumer);
}
import java.lang.reflect.Field;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;