Scala: map just first element in tuple

时间:2017-03-30 18:51:47

标签: scala tuples

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?

3 个答案:

答案 0 :(得分:6)

您可以使用无形元组函数:

import shapeless._
import syntax.std.tuple._
val x=(1,2,3)
val y=x.updatedAt(0,x.head*5)
// y= (5,2,3)

答案 1 :(得分:2)

我们可以使用_1作为元组字段。还有一个解决方案,只需使用标准库:

val x = (1, 2, 3)
val y = x.copy(_1 = x._1 * 5)

答案 2 :(得分:1)

你可以使用case类而不是元组(这是好的样式)然后使用命名元素,例如

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)