Skip to content

Commit 2d9c1ec

Browse files
author
Antoine Brunner
committed
Create a definition of the Split[T, N] type
1 parent debe978 commit 2d9c1ec

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

library/src/scala/Tuple.scala

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ object Tuple {
6565
}
6666

6767
/** Type of the concatenation of two tuples */
68-
type Concat[X <: Tuple, +Y <: Tuple] <: Tuple = X match {
68+
type Concat[+X <: Tuple, +Y <: Tuple] <: Tuple = X match {
6969
case Unit => Y
7070
case x1 *: xs1 => x1 *: Concat[xs1, Y]
7171
}
@@ -116,6 +116,29 @@ object Tuple {
116116
*/
117117
type IsMappedBy[F[_]] = [X <: Tuple] =>> X =:= Map[InverseMap[X, F], F]
118118

119+
/** Transforms a tuple `(T1, ..., Tn)` into `(T1, ..., Ti)`. */
120+
type Take[+T <: Tuple, N <: Int] = N match {
121+
case 0 => Unit
122+
case S[n1] => T match {
123+
case Unit => Unit
124+
case x *: xs => Concat[x *: Unit, Take[xs, n1]]
125+
}
126+
}
127+
128+
/** Transforms a tuple `(T1, ..., Tn)` into `(Ti+1, ..., Tn)`. */
129+
type Drop[+T <: Tuple, N <: Int] = N match {
130+
case 0 => T
131+
case S[n1] => T match {
132+
case Unit => Unit
133+
case x *: xs => Drop[xs, n1]
134+
}
135+
}
136+
137+
/** Splits a tuple (T1, ..., Tn) into a pair of two tuples `(T1, ..., Ti)` and
138+
* `(Ti+1, ..., Tn)`.
139+
*/
140+
type Split[+T <: Tuple, N <: Int] = (Take[T, N], Drop[T, N]) = (Take[T, N], Drop[T, N])
141+
119142
/** Convert an array into a tuple of unknown arity and types */
120143
def fromArray[T](xs: Array[T]): Tuple = {
121144
val xs2 = xs match {

0 commit comments

Comments
 (0)