Created
June 24, 2022 01:09
-
-
Save guymers/3de4b9d67184e87537f4ea2ec1d90ac6 to your computer and use it in GitHub Desktop.
Scala 3 tuple repeat
This file contains hidden or 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.* | |
| import scala.compiletime.ops.int.* | |
| type RepeatN[N <: Int, A] <: Tuple = N match { | |
| case 0 => EmptyTuple | |
| case S[n] => A *: RepeatN[n, A] | |
| } | |
| def repeat[N <: Int, A](n: N)(a: A): RepeatN[n.type, A] = { | |
| val tuple = n match { | |
| case n if n <= 0 => EmptyTuple | |
| case n => a *: repeat(n - 1)(a) | |
| } | |
| tuple.asInstanceOf[RepeatN[n.type, A]] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment