Skip to content

Commit 07c5ba1

Browse files
committed
Add scala.Tuple.Fold
1 parent ffa9490 commit 07c5ba1

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

library/src/scala/Tuple.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ object Tuple {
103103
case x *: xs => S[Size[xs]]
104104
}
105105

106+
/** Fold a tuple `(T1, ..., Tn)` into `F[T1, F[... F[Tn, Z]...]]]` */
107+
type Fold[T <: Tuple, Z, F[_, _]] = T match
108+
case EmptyTuple => Z
109+
case h *: t => F[h, Fold[t, Z, F]]
110+
106111
/** Converts a tuple `(T1, ..., Tn)` to `(F[T1], ..., F[Tn])` */
107112
type Map[Tup <: Tuple, F[_]] <: Tuple = Tup match {
108113
case EmptyTuple => EmptyTuple

tests/pos/tuple-fold.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
type Empty[X] = EmptyTuple
3+
type Twice[X] = (X, X)
4+
5+
def test =
6+
val a1: EmptyTuple = ??? : Tuple.Fold[EmptyTuple, Nothing, Tuple2]
7+
val a2: (Int, (String, Nothing)) = ??? : Tuple.Fold[(Int, String), Nothing, Tuple2]
8+
val a3: Int | String | Char = ??? : Tuple.Fold[(Int, String, Char), Nothing, [X, Y] =>> X | Y]

0 commit comments

Comments
 (0)