Skip to content

Implement Tuple.FlatMap #13245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
8 changes: 8 additions & 0 deletions library/src/scala/Tuple.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scala
import annotation.showAsInfix
import compiletime._
import compiletime.ops.int._
import scala.annotation.experimental

/** Tuple of arbitrary arity */
sealed trait Tuple extends Product {
Expand Down Expand Up @@ -54,6 +55,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]]

/** Called on a tuple `(a1, ..., an)`, returns a new tuple `f(a1) ++ ... ++ f(an)`.
* The result is typed as `Concat[F[A1], ...Concat[F[An], EmptyTuple]...])` if the tuple type is fully known.
*/
@experimental
inline def flatMap[This >: this.type <: Tuple, F[_] <: Tuple](f: [t] => t => F[t]): FlatMap[This, F] =
Copy link
Contributor

Choose a reason for hiding this comment

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

This signature is not ideal as we need to pass the tuple type when we call. This should always be inferred.
Maybe we should wait until we can do something like

  inline def flatMap[F[_] <: Tuple][This >: this.type <: Tuple](f: [t] => t => F[t]): FlatMap[This, F] =

runtime.Tuples.flatMap(this, f).asInstanceOf[FlatMap[This, F]]

/** Given a tuple `(a1, ..., am)`, returns the tuple `(a1, ..., an)` consisting
* of its first n elements.
*/
Expand Down
5 changes: 5 additions & 0 deletions library/src/scala/runtime/Tuples.scala
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,11 @@ object Tuples {
case _ => fromIArray(self.productIterator.map(f(_).asInstanceOf[Object]).toArray.asInstanceOf[IArray[Object]]) // TODO use toIArray
}

def flatMap[F[_] <: Tuple](self: Tuple, f: [t] => t => F[t]): Tuple = self match {
case EmptyTuple => self
case _ => fromIArray(self.toIArray.flatMap(f(_).toIArray))
}

def take(self: Tuple, n: Int): Tuple = {
if (n < 0) throw new IndexOutOfBoundsException(n.toString)
val selfSize: Int = self.size
Expand Down
3 changes: 3 additions & 0 deletions project/MiMaFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ object MiMaFilters {
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.getJPath"),
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.name"),
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.path"),

// New APIs marked @experimental in 3.1.0
exclude[DirectMissingMethodProblem]("scala.runtime.Tuples.flatMap"),
)
}
4 changes: 4 additions & 0 deletions tests/run/TupleFlatMap.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
()
(1,1,2,2,x,x,d,d)
()
(1,1!,x,x!)
19 changes: 19 additions & 0 deletions tests/run/TupleFlatMap.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@main def Test = {

println(
Tuple().flatMap[EmptyTuple, [t] =>> (t, t)]([t] => (x: t) => (x, x))
)

println(
(1, 2, "x", "d").flatMap[(Int, Int, String, String), [t] =>> (t, t)]([t] => (x: t) => (x, x))
)

println(
(1, "x").flatMap[(Int, String), [t] =>> EmptyTuple]([t] => (x: t) => Tuple())
)

println(
(1, "x").flatMap[(Int, String), [t] =>> (t, String)]([t] => (x: t) => (x, x.toString + "!"))
)

}