Skip to content

Tuple methods #12888

Jun 21, 2021 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

One way to implement it is like this where we perform the operations on arrays.

import Tuple.FlatMap

extension (tup: Tuple)
  def flatMap[F[_] <: Tuple](f: [t] => t => F[t]): FlatMap[tup.type, F] =
    flatMapOnUnderlyingArrays(tup, f).asInstanceOf[FlatMap[tup.type, F]]

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

def tests =
  val tup1: (Int, Int, String, String, Double, Double) = (1, "a", 3.0).flatMap[[t] =>> (t, t)]([t] => (x: t) => (x, x))
  ()

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@nicolasstucki
Comment options

Answer selected by nicolasstucki
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants