Skip to content

Instantly share code, notes, and snippets.

View rshepherd's full-sized avatar
💭
Building something…

Randy Shepherd rshepherd

💭
Building something…
View GitHub Profile
public class IdentitySwitch {
public static void main(String[] args) {
String s = "unchanged";
System.out.println(s);
foo(s);
System.out.println(s);
}
public void foo(String s) {
s = "changed"
class DoublePointer {
String s;
public DoublePointer(String s){
this.s = s;
}
}
public class IdentitySwitch {
public static void main(String[] args) {
DoublePointer dp = new DoublePointer("unchanged");
@rshepherd
rshepherd / SkipList.kt
Last active February 19, 2025 21:23
A small portion of a toy SkipList with polymorphic find for instructional purposes
class SkipList {
data class Node<Value>(val id: Value, val children: MutableList<Node<Value>> = mutableListOf())
abstract class Value<T>(val value: T) {
abstract fun distance(other: T): Int
}
class IntValue(id: Int) : Value<Int>(id) {
override fun distance(other: Int): Int = kotlin.math.abs(this.value - other)
@rshepherd
rshepherd / somecattheory.scala
Last active March 5, 2025 21:00
Functors, Natural Transformations, Monoids & Preorders
// A common example of a functor in Scala is the Option type.
// In Scala, Option is a functor because it provides the map method, which allows you to
// apply a function to the value inside an Option while preserving its structure.
val someNumber: Option[Int] = Some(3)
val incremented: Option[Int] = someNumber.map(_ + 1)
println(incremented) // Value transformed while structure is preserved
// Similarly List is a Functor
val numbers = List(1, 2, 3)
val doubled = numbers.map(_ * 2)