Skip to content

Instantly share code, notes, and snippets.

View lukeknxt's full-sized avatar

Luke lukeknxt

View GitHub Profile
@lukeknxt
lukeknxt / any.kt
Last active January 30, 2024 21:48
Kotlin happily infers Any for you
data class Wrapper(val id: String)
val ids = listOf("id1", "id2")
listOf(
Wrapper("id1").id,
Wrapper("id2")
).forEach { someId -> // inferred as Any
val result = ids.find { it == someId }
// id2 will never be found, but the compiler won't tell you
}
@lukeknxt
lukeknxt / shallow_clone.py
Last active January 10, 2022 02:41
Shallow clone all tables to another schema in a Databricks environment
"""
Use-case: automate clones in Databricks for dev testing similar to Snowflake's zero-copy clone.
This doesn't use the Catalog API as that may rely on cred passthrough
- https://docs.microsoft.com/en-us/azure/databricks/security/credential-passthrough/adls-passthrough
`py4j.security.Py4JSecurityException: method ... is not whitelisted on class org.apache.spark.sql.catalog.Table`
"""
prefixes = ["layer_a", "layer_b"] # prefix matching on schemas to copy over for layers
@lukeknxt
lukeknxt / enums_and_unions.ts
Created December 2, 2021 10:39
Unions don't always type with enums!
enum E {
A = "A",
B = "B",
}
const yep: "A" = E.A;
const yep2: E = E.A;
const nope: E = "A"; // why though
@lukeknxt
lukeknxt / hmm.ts
Created November 25, 2021 12:00
WTF TypeScript
interface Service {
update(obj: { id?: string }): void;
// changing this to a function type `update: (obj: {id?: string}) => void` works
// and causes the implementation not to type-check
}
class ServiceImpl implements Service {
// somehow this is a valid implementation
update(obj: { id: string }): void {
// `id` can never be undefined or null here according to the type system
// scala version
def mostFrequent(arr: Array[Int]): Int = {
arr.groupBy(identity).maxBy(_._2.size)._1
}
// java version (from https://www.geeksforgeeks.org/frequent-element-array/)
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;