Skip to content

Commit 9772ac3

Browse files
Add Filter type to the tuple
1 parent e67908d commit 9772ac3

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

library/src/scala/Tuple.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ object Tuple {
125125
case h *: t => Concat[F[h], FlatMap[t, F]]
126126
}
127127

128+
/** Filters out those members of the tuple for which the predicate `P` returns `false`. */
129+
type Filter[Tup <: Tuple, P[_] <: Boolean] <: Tuple = Tup match {
130+
case EmptyTuple => EmptyTuple
131+
case h *: t => P[h] match {
132+
case true => h *: Filter[t, P]
133+
case false => Filter[t, P]
134+
}
135+
}
136+
128137
/** Given two tuples, `A1 *: ... *: An * At` and `B1 *: ... *: Bn *: Bt`
129138
* where at least one of `At` or `Bt` is `EmptyTuple` or `Tuple`,
130139
* returns the tuple type `(A1, B1) *: ... *: (An, Bn) *: Ct`

tests/neg/tuple-filter.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type P[x] <: Boolean = x match {
2+
case 3 => false
3+
case _ => true
4+
}
5+
val tup: (1, 2, 3) = (1, 2, 3)
6+
val tup2: Tuple.Filter[(1, 2, 3, 4), P] = tup // error

tests/pos/tuple-filter.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type P[x] <: Boolean = x match {
2+
case 3 => false
3+
case _ => true
4+
}
5+
val tup: (1, 2, 4) = (1, 2, 4)
6+
val tup2: Tuple.Filter[(1, 2, 3, 4), P] = tup

0 commit comments

Comments
 (0)