forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16463.scala
43 lines (34 loc) · 1.03 KB
/
16463.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//> using scala "3.2.1"
import scala.compiletime.ops.int._
object TupleOps {
import Tuple._
type Reduce[T <: NonEmptyTuple, F[_, _]] =
Fold[Tuple.Tail[T], Tuple.Head[T], F]
type Maximum[T <: NonEmptyTuple] = Reduce[
T,
[A, B] =>> (A, B) match {
case (Int, Int) => A `Max` B
}
]
type IndexOfRec[T <: Tuple, Elem, I <: Int] = Tuple.Elem[T, I] match {
case Elem => I
case _ => IndexOfRec[T, Elem, I + 1]
}
type IndexOf[T <: Tuple, Elem] = IndexOfRec[T, Elem, 0]
type DropLargest[T <: NonEmptyTuple] =
T `IndexOf` Maximum[T] match {
case Int =>
(
(T `Take` (T `IndexOf` Maximum[T])) `Concat`
(T `Drop` ((T `IndexOf` Maximum[T]) + 1))
) *: EmptyTuple
}
type BubbleSort[T <: Tuple] = T match {
case EmptyTuple => EmptyTuple
case NonEmptyTuple =>
BubbleSort[DropLargest[T]] `Concat` (Maximum[T] *: EmptyTuple)
}
}
object demo extends App {
println(compiletime.constValue[TupleOps.BubbleSort[(1, 2)]]) // error: Recursion limit exceeded
}