4

I want to use some function on tuple, which returns tuple with only first element transformed and other elements unchanged.

This is naive version for Tuple2:

def mapFirst[T, U, R](tuple: (T, U))(f: T => R): (R, U) = tuple match {
     |   case (x, y) => f(x) -> y
     | }

mapFirst((1, 2))(_ * 5) // returns (5, 2)

Though, it doesn't feel native, what I want is to write it this way:

(1, 2).mapFirst(_ * 5)

I wrote implicit conversion then:

class RichTuple[T, U](tuple: (T, U)) {
  def mapFirst[R](f: T => R): (R, U) = tuple match {
    case (x, y) => f(x) -> y
  }
}

implicit def tuple2RichTuple[T, U](tuple: (T, U)): RichTuple[T, U] = new RichTuple(tuple)

(1, 2).mapFirst(_ * 5)

Then, when I'll want to map just second element or first element on Tuple3, I'll have to write same boilerplate again. Is there some library which has such methods built-in?

Evgeny Veretennikov
  • 3,889
  • 2
  • 16
  • 36

3 Answers3

7

You can use shapeless tuple functions:

import shapeless._
import syntax.std.tuple._
val x=(1,2,3)
val y=x.updatedAt(0,x.head*5)
// y= (5,2,3)
Arnon Rotem-Gal-Oz
  • 25,469
  • 3
  • 45
  • 68
4

We are able to use _1 as tuple's field. One more solution, just using standard library:

val x = (1, 2, 3)
val y = x.copy(_1 = x._1 * 5)
Evgeny Veretennikov
  • 3,889
  • 2
  • 16
  • 36
  • Unfortunately, this doesn't work as well if `x` is an expression instead of a variable. – DBear Jun 25 '22 at 20:22
1

You could use case classes rather than tuples (this is good style) then use the named elements, e.g.

case class Pie(filling: String, weight: Double, servings: Int)
val p = Pie("cheese", 500.0, 2)
val q = p.copy(filling = "potato")
val r = p.copy(weight = 750.0, servings = 3)
Luigi Plinge
  • 50,650
  • 20
  • 113
  • 180