Last active
September 7, 2024 15:14
-
-
Save johnynek/0be5b05e10c30267a9381b28894df6da to your computer and use it in GitHub Desktop.
Implementation of linked list using dotty features (opaque type, union types, extension methods, type lambda).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// unfortunately | |
// opaque type Fix[F[_]] = F[Fix[F]] | |
// won't work (no recursion in opaque type), but this implementation is safe, but scary due to asInstanceOf | |
object FixImpl { | |
type Fix[F[_]] | |
inline def fix[F[_]](f: F[Fix[F]]): Fix[F] = f.asInstanceOf[Fix[F]] | |
inline def unfix[F[_]](f: Fix[F]): F[Fix[F]] = f.asInstanceOf[F[Fix[F]]] | |
} | |
import FixImpl._ | |
type OrNull[A <: AnyRef] = A | Null | |
// List[A] = null | (A, null) | (A, (A, null)) | (A, (A, (A, null))) | ... | |
opaque type List[A] = Fix[[X] =>> OrNull[(A, X)]] | |
object List { | |
def empty[A]: List[A] = fix(null) | |
} | |
given ListOps { | |
def (head: A) ::[A](self: List[A]): List[A] = | |
fix((head, self)) | |
def (self: List[A]) isEmpty[A] = unfix(self) == null | |
def (self: List[A]) headOption[A] = { | |
unfix(self) match { | |
case null => None | |
case (a, _) => Some(a) | |
} | |
} | |
@annotation.tailrec | |
def (self: List[A]) foldLeft[A, B](init: B)(fn: (B, A) => B): B = | |
unfix(self) match { | |
case null => init | |
case (a, rest) => rest.foldLeft(fn(init, a))(fn) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment