Skip to content

Instantly share code, notes, and snippets.

@guymers
Created June 24, 2022 01:09
Show Gist options
  • Select an option

  • Save guymers/3de4b9d67184e87537f4ea2ec1d90ac6 to your computer and use it in GitHub Desktop.

Select an option

Save guymers/3de4b9d67184e87537f4ea2ec1d90ac6 to your computer and use it in GitHub Desktop.
Scala 3 tuple repeat
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