Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions library/src/scala/Tuple.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ sealed trait Tuple extends Product {
inline def map[F[_]](f: [t] => t => F[t]): Map[this.type, F] =
runtime.Tuples.map(this, f).asInstanceOf[Map[this.type, F]]

/** Given a tuple of form `(F[T1], F[T2], ..., F[Tn])` and a function from F[T] to G[T] for any T out of T1...Tn,
* returns a new tuple `(G[T1], G[T2], ..., G[Tn])`.
*/
inline def mapKind[F[_], G[_]](fk: [t <: Union[InverseMap[this.type, F]]] => F[t] => G[t]): MapKind[this.type, F, G] =
runtime.Tuples.map(this, fk.asInstanceOf).asInstanceOf[MapKind[this.type, F, G]]


/** Given a tuple `(a1, ..., am)`, returns the tuple `(a1, ..., an)` consisting
* of its first n elements.
*/
Expand Down Expand Up @@ -220,6 +227,9 @@ object Tuple {
case EmptyTuple => EmptyTuple
}

/** Converts a tuple `(F[T1], F[T2], ..., F[Tn])` to `(G[T1], G[T2], ..., G[Tn])` */
type MapKind[T <: Tuple, F[_], G[_]] = Map[InverseMap[T, F], G]

/** Implicit evidence. IsMappedBy[F][X] is present in the implicit scope iff
* X is a tuple for which each element's type is constructed via `F`. E.g.
* (F[A1], ..., F[An]), but not `(F[A1], B2, ..., F[An])` where B2 does not
Expand Down
39 changes: 39 additions & 0 deletions tests/run/Tuple-mapKind.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
object Test {

type Tup = (Int, Double, Boolean, String)

def main(args: Array[String]): Unit = {

val headOption: [x] => Seq[x] => Option[x] = [x] => (seq: Seq[x]) => seq.headOption
val firstTruthy: [T <: Tuple.Union[Tup]] => Seq[T] => Option[T] = [T <: Tuple.Union[Tup]] =>
(seq: Seq[T]) => seq.find {
case x: Int => x > 0
case x: Double => x > 0.0
case x: Boolean => x
case x: String => x.nonEmpty
}

val seqTuple: Tuple.Map[Tup, Seq] = (Vector(0, 1), Seq.empty, List(false, true), Vector("4", "44"))
val expectHeadOptionTuple: Tuple.Map[Tup, Option] = (Some(0), None, Some(false), Some("4"))
val expectFirstTruthyTuple: Tuple.Map[Tup, Option] = (Some(1), None, Some(true), Some("4"))

assert(
EmptyTuple.mapKind[Seq, Option](headOption) == EmptyTuple
)
assert(
EmptyTuple.mapKind[Seq, Option](firstTruthy) == EmptyTuple
)
assert(
(List("", "1") *: EmptyTuple).mapKind[Seq, Option](headOption) == (Some("") *: EmptyTuple)
)
assert(
(List("", "1") *: EmptyTuple).mapKind[Seq, Option](firstTruthy) == (Some("1") *: EmptyTuple)
)
assert(
seqTuple.mapKind[Seq, Option](headOption) == expectHeadOptionTuple
)
assert(
seqTuple.mapKind[Seq, Option](firstTruthy) == expectFirstTruthyTuple
)
}
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got the following pickling comparison error (diff before-pickling.txt after-pickling.txt)

172,173c172,173
<             :[T <: scala.Int | scala.Double | (scala.Boolean | String)] => (seq: Seq[T]) => scala.Option[T]>@
<               tests/run/Tuple-mapKind.scala<261..458>
---
>             :[T <: scala.Tuple.Fold[(scala.Int, scala.Double, scala.Boolean, String), scala.Nothing, [x, y] =>> x | y]
>               ] => (seq: Seq[T]) => scala.Option[T]>@tests/run/Tuple-mapKind.scala<261..458>
404,405c404,405
<                         <firstTruthy:[T <: scala.Int | scala.Double | (scala.Boolean | scala.Predef.String)] => (x$1:
<                           Seq[T]) => scala.Option[T]>@tests/run/Tuple-mapKind.scala<889..900>
---
>                         <firstTruthy:[T <: scala.Tuple.Fold[Test.Tup, scala.Nothing, [x, y] =>> x | y]] => (x$1: Seq[T]
>                           ) => scala.Option[T]>@tests/run/Tuple-mapKind.scala<889..900>
614,615c614,615
<                         <firstTruthy:[T <: scala.Int | scala.Double | (scala.Boolean | scala.Predef.String)] => (x$1:
<                           Seq[T]) => scala.Option[T]>@tests/run/Tuple-mapKind.scala<1106..1117>
---
>                         <firstTruthy:[T <: scala.Tuple.Fold[Test.Tup, scala.Nothing, [x, y] =>> x | y]] => (x$1: Seq[T]
>                           ) => scala.Option[T]>@tests/run/Tuple-mapKind.scala<1106..1117>
678,682c678,679
<                                   scala.Tuple.InverseMap[
<                                     (seqTuple : (Seq[scala.Int], Seq[scala.Double], Seq[scala.Boolean],
<                                       Seq[scala.Predef.String])),
<                                   F],
<                                 scala.Nothing, [x, y] =>> x | y]
---
>                                   scala.Tuple.InverseMap[(seqTuple : scala.Tuple.Map[Test.Tup, scala.package.Seq]), F],
>                                   scala.Nothing, [x, y] =>> x | y]
685,689c682,683
<                                   scala.Tuple.InverseMap[
<                                     (seqTuple : (Seq[scala.Int], Seq[scala.Double], Seq[scala.Boolean],
<                                       Seq[scala.Predef.String])),
<                                   F],
<                                 G]
---
>                                   scala.Tuple.InverseMap[(seqTuple : scala.Tuple.Map[Test.Tup, scala.package.Seq]), F],
>                                   G]
696,699c690,691
<                               scala.Tuple.InverseMap[
<                                 (seqTuple : (Seq[scala.Int], Seq[scala.Double], Seq[scala.Boolean],
<                                   Seq[scala.Predef.String])),
<                               scala.package.Seq],
---
>                               scala.Tuple.InverseMap[(seqTuple : scala.Tuple.Map[Test.Tup, scala.package.Seq]),
>                                 scala.package.Seq],
703,706c695,696
<                               scala.Tuple.InverseMap[
<                                 (seqTuple : (Seq[scala.Int], Seq[scala.Double], Seq[scala.Boolean],
<                                   Seq[scala.Predef.String])),
<                               scala.package.Seq],
---
>                               scala.Tuple.InverseMap[(seqTuple : scala.Tuple.Map[Test.Tup, scala.package.Seq]),
>                                 scala.package.Seq],
749,753c739,740
<                                   scala.Tuple.InverseMap[
<                                     (seqTuple : (Seq[scala.Int], Seq[scala.Double], Seq[scala.Boolean],
<                                       Seq[scala.Predef.String])),
<                                   F],
<                                 scala.Nothing, [x, y] =>> x | y]
---
>                                   scala.Tuple.InverseMap[(seqTuple : scala.Tuple.Map[Test.Tup, scala.package.Seq]), F],
>                                   scala.Nothing, [x, y] =>> x | y]
756,760c743,744
<                                   scala.Tuple.InverseMap[
<                                     (seqTuple : (Seq[scala.Int], Seq[scala.Double], Seq[scala.Boolean],
<                                       Seq[scala.Predef.String])),
<                                   F],
<                                 G]
---
>                                   scala.Tuple.InverseMap[(seqTuple : scala.Tuple.Map[Test.Tup, scala.package.Seq]), F],
>                                   G]
767,770c751,752
<                               scala.Tuple.InverseMap[
<                                 (seqTuple : (Seq[scala.Int], Seq[scala.Double], Seq[scala.Boolean],
<                                   Seq[scala.Predef.String])),
<                               scala.package.Seq],
---
>                               scala.Tuple.InverseMap[(seqTuple : scala.Tuple.Map[Test.Tup, scala.package.Seq]),
>                                 scala.package.Seq],
774,777c756,757
<                               scala.Tuple.InverseMap[
<                                 (seqTuple : (Seq[scala.Int], Seq[scala.Double], Seq[scala.Boolean],
<                                   Seq[scala.Predef.String])),
<                               scala.package.Seq],
---
>                               scala.Tuple.InverseMap[(seqTuple : scala.Tuple.Map[Test.Tup, scala.package.Seq]),
>                                 scala.package.Seq],
782,783c762,763
<                         <firstTruthy:[T <: scala.Int | scala.Double | (scala.Boolean | scala.Predef.String)] => (x$1:
<                           Seq[T]) => scala.Option[T]>@tests/run/Tuple-mapKind.scala<1293..1304>
---
>                         <firstTruthy:[T <: scala.Tuple.Fold[Test.Tup, scala.Nothing, [x, y] =>> x | y]] => (x$1: Seq[T]
>                           ) => scala.Option[T]>@tests/run/Tuple-mapKind.scala<1293..1304>

Loading