Skip to content

Instantly share code, notes, and snippets.

View lemnik's full-sized avatar

Jason lemnik

View GitHub Profile
@lemnik
lemnik / InstanceFunctionReferences.kt
Created October 11, 2018 07:32
Using instance-function references in Kotlin
// https://pl.kotl.in/rJgInO29m
class MyClass {
private fun doThing1(name: String) =
println("Thing1 says: Hello $name")
private fun doThing2(name: String) =
println("Thing2 says: Hi $name!")
private fun fail(name: String) =
@lemnik
lemnik / docker-clear.sh
Created November 23, 2018 08:50
Clear all Docker containers and volumes
#!/bin/bash
docker container rm `docker container ls -a | cut -d " " -f1 | tail -n +2`; docker volume prune -f
@lemnik
lemnik / HuffmanTree.kt
Created April 29, 2020 06:32
Simple HuffmanTree / HuffmanTable implementation in Kotlin
import java.util.*
class HuffmanTree<T> {
private val root: Node<T>
constructor(vararg elements: Node<T>) : this(elements.toList())
constructor(elements: Collection<Node<T>>) {
val queue = PriorityQueue(elements)
while (queue.size > 1) {
// JVM Delegate to expose a private field, and bypass any Kotlin getters & setters
// This is a horrible abuse of reflection and should never be used, unless you absolutely have to have it.
import java.lang.reflect.Field
import kotlin.reflect.KProperty
class ExposeDelegate<T> {
private fun field(thisRef: Any?, property: KProperty<*>): Field? {
if (thisRef == null) return null
val name = property.name
@lemnik
lemnik / sigalloc.c
Created November 17, 2021 07:58
mmap with some housekeeping
#include <unistd.h>
#include <sys/mman.h>
// MEMORY_SANITIZER checks shamelessly stolen from Breakpad: src/common/memory_allocator.h
#if defined(MEMORY_SANITIZER)
#include <sanitizer/msan_interface.h>
#endif
#include "sigalloc.h"