Skip to content

Instantly share code, notes, and snippets.

import scala.compiletime.ops.int._
sealed trait Lst[+A, N <: Int]
case object Nil extends Lst[Nothing, 0]
case class Cons[+A, N <: Int](head: A, tail: Lst[A, N]) extends Lst[A, N + 1]
def map2[A, B, C, N <: Int](list1: Lst[A, N], list2: Lst[B, N])(f: (A, B) => C): Lst[C, N] =
(list1, list2) match
case (Nil, Nil) => Nil
case (Cons(h1, t1), Cons(h2, t2)) =>
inline def f(x: Any) =
inline x match
case Int => "Int"
@main def main = println:
f("a")
object A:
def unapplySeq(x: String) = Some(x.toSeq)
@main def main = println:
"Scala" match
case A(a, b, _*) => s"$a, $b"
@main def main =
def summon2[T](using x: T): x.type = x
println(
summon[Numeric[Int]] ==
summon2[Numeric[Int]]
)
import scala.deriving.Mirror
trait Show[A]:
def show(a: A): String
object Show:
def derived[A](using m: Mirror.ProductOf[A]): Show[A] =
new Show[A]:
def show(a: A): String = m.fromProduct(a.asInstanceOf[Product]).toString
class A
object A:
given A = A()
@main def main = println(summon[A])
class Nat
case class Add(val l: Nat, val r: Nat)
extension (a: Add)
def comm = Add(a.r, a.l)
@main def main =
val n = Nat()
val m = Nat()
import scala.collection.mutable.ArrayBuffer
type A = ArrayBuffer[Int]
def builder (init: A ?=> Unit) =
given e: A = new A
init
e
def add(x: Int)(using e: A) =
class A
class B extends A
val f: B => A = new Function1[A, B]{
def apply(a: A): B = B()
}
val g: B => A = (a: A) => B()
@main def main =
println(f(B()))
type A = [T] =>> T match { case String => Int; case Int => String }
def f1[T](a: T): A[T] = a match
case a: String => a.toInt
case a: Int => a.toString
def f2[T](a: T): T match { case String => Int; case Int => String } = a match
case a: String => a.toInt
case a: Int => a.toString
def f3[T](a: T) = a match