Skip to content

Commit ffa9490

Browse files
committed
Add scala.Tuple.FlatMap
1 parent de75713 commit ffa9490

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

library/src/scala/Tuple.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ object Tuple {
109109
case h *: t => F[h] *: Map[t, F]
110110
}
111111

112+
/** Converts a tuple `(T1, ..., Tn)` to a flattened `(..F[T1], ..., ..F[Tn])` */
113+
type FlatMap[Tup <: Tuple, F[_] <: Tuple] <: Tuple = Tup match {
114+
case EmptyTuple => EmptyTuple
115+
case h *: t => Concat[F[h], FlatMap[t, F]]
116+
}
117+
112118
/** Given two tuples, `A1 *: ... *: An * At` and `B1 *: ... *: Bn *: Bt`
113119
* where at least one of `At` or `Bt` is `EmptyTuple` or `Tuple`,
114120
* returns the tuple type `(A1, B1) *: ... *: (An, Bn) *: Ct`

tests/pos/tuple-flatmap.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
type Empty[X] = EmptyTuple
3+
type Twice[X] = (X, X)
4+
5+
def test =
6+
val a1: EmptyTuple = ??? : Tuple.FlatMap[EmptyTuple, Empty]
7+
val a2: EmptyTuple = ??? : Tuple.FlatMap[(Int, String), Empty]
8+
9+
val b1: EmptyTuple = ??? : Tuple.FlatMap[EmptyTuple, Tuple1]
10+
val b2: (Int, String) = ??? : Tuple.FlatMap[(Int, String), Tuple1]
11+
12+
val c1: EmptyTuple = ??? : Tuple.FlatMap[EmptyTuple, Twice]
13+
val c2: (Int, Int, String, String) = ??? : Tuple.FlatMap[(Int, String), Twice]
14+
val c3: (Int, List[Int], String, List[String]) = ??? : Tuple.FlatMap[(Int, String), [X] =>> (X, List[X])]

0 commit comments

Comments
 (0)