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
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)) => |
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
inline def f(x: Any) = | |
inline x match | |
case Int => "Int" | |
@main def main = println: | |
f("a") |
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
object A: | |
def unapplySeq(x: String) = Some(x.toSeq) | |
@main def main = println: | |
"Scala" match | |
case A(a, b, _*) => s"$a, $b" |
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
@main def main = | |
def summon2[T](using x: T): x.type = x | |
println( | |
summon[Numeric[Int]] == | |
summon2[Numeric[Int]] | |
) |
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
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 |
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
class A | |
object A: | |
given A = A() | |
@main def main = println(summon[A]) |
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
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() |
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
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) = |
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
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())) |
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
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 |
NewerOlder