Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Last active February 21, 2021 06:29
Show Gist options
  • Save xuwei-k/924c69482c972d7bac496922ed725f79 to your computer and use it in GitHub Desktop.
Save xuwei-k/924c69482c972d7bac496922ed725f79 to your computer and use it in GitHub Desktop.
scalaVersion := "3.0.0-RC1"
import scala.compiletime.ops.int._
sealed trait HList {
def ::[H](head: H): head.type :: this.type = new ::[head.type, this.type](head, this)
}
final case class ::[+H, +T <: HList](head: H, tail: T) extends HList
sealed trait HNil extends HList
case object HNil extends HNil
object MatchTypeSort {
type LTs[A <: HList, B <: Int] <: HList = A match {
case HNil =>
HNil
case t :: ts =>
(t < B) match {
case true =>
t :: LTs[ts, B]
case false =>
LTs[ts, B]
}
}
type GTs[A <: HList, B <: Int] <: HList = A match {
case HNil =>
HNil
case t :: ts =>
(t > B) match {
case true =>
t :: GTs[ts, B]
case false =>
GTs[ts, B]
}
}
def typed[A](a: A): Unit = ()
type HListConcat[A <: HList, B <: HList] <: HList = A match {
case HNil =>
B
case h :: t =>
h :: (HListConcat[t, B])
}
type Sort[A <: HList] <: HList = A match {
case HNil =>
HNil
case h :: t =>
HListConcat[Sort[LTs[t, h]], h :: Sort[GTs[t, h]]]
}
def main(args: Array[String]): Unit = {
typed[Sort[5 :: 3 :: 7 :: 2 :: 1 :: 6 :: 4 :: HNil]](
1 :: 2 :: 3 :: 4 :: 5 :: 6 :: 7 :: (HNil: HNil)
)
}
}
addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % "0.5.3")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment