File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 1+ //> using scala " 3.2.1"
2+
3+ import scala .compiletime .ops .int ._
4+
5+ object TupleOps {
6+ import Tuple ._
7+
8+ type Reduce [T <: NonEmptyTuple , F [_, _]] =
9+ Fold [Tuple .Tail [T ], Tuple .Head [T ], F ]
10+
11+ type Maximum [T <: NonEmptyTuple ] = Reduce [
12+ T ,
13+ [A , B ] =>> (A , B ) match {
14+ case (Int , Int ) => A `Max` B
15+ }
16+ ]
17+
18+ type IndexOfRec [T <: Tuple , Elem , I <: Int ] = Tuple .Elem [T , I ] match {
19+ case Elem => I
20+ case _ => IndexOfRec [T , Elem , I + 1 ]
21+ }
22+
23+ type IndexOf [T <: Tuple , Elem ] = IndexOfRec [T , Elem , 0 ]
24+
25+ type DropLargest [T <: NonEmptyTuple ] =
26+ T `IndexOf` Maximum [T ] match {
27+ case Int =>
28+ (
29+ (T `Take` (T `IndexOf` Maximum [T ])) `Concat`
30+ (T `Drop` ((T `IndexOf` Maximum [T ]) + 1 ))
31+ ) *: EmptyTuple
32+ }
33+
34+ type BubbleSort [T <: Tuple ] = T match {
35+ case EmptyTuple => EmptyTuple
36+ case NonEmptyTuple =>
37+ BubbleSort [DropLargest [T ]] `Concat` (Maximum [T ] *: EmptyTuple )
38+ }
39+ }
40+
41+ object demo extends App {
42+ println(compiletime.constValue[TupleOps .BubbleSort [(1 , 2 )]]) // error: Recursion limit exceeded
43+ }
You can’t perform that action at this time.
0 commit comments