Reputation: 4239
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?
Upvotes: 4
Views: 2084
Reputation: 4239
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)
Upvotes: 4
Reputation: 51109
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)
Upvotes: 1
Reputation: 25909
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)
Upvotes: 7