I’m having a hard time defining this question. I want to do something like this:
val list = List(("a",1), ("b",2))
private def shiftIndex[C <: Seq[_]](seq: C, amount: Int): C = {
seq.map({ case (value, integer) => (value, integer + amount) })
}
shiftIndex(list,3)
Should return
List(("a",4),("b",5)): List[String,Integer]
But it should work for the most general case:
- seq implements map (I know its not :Seq)
- seq is storing a tuple of anything and an Int.
Any links to resources the explains these concepts would be appreciated.
Though I might recommend a solution that’s nicer looking and more generic. Here I enrich GenTraversable so that all traversable objects of pairs have a method
mapValsthat takes a function to be applied to the second part of the pair, and has a return type the same as the object it was run on: