scala-collection-contrib
scala-collection-contrib copied to clipboard
Collection-like operations on tuples
Sometimes I work with homogenous tuples, which I use more like a collection with a fixed size. When working in this style, I often define a map extension for tuples. Would something like this be worth adding here?
Example:
implicit class AnyTuple2Ops[T](v: (T, T)) {
def map[X](f: T => X): (X, X) = (f(v._1), f(v._2))
}
implicit class AnyTuple3Ops[T](v: (T, T, T)) {
def map[X](f: T => X): (X, X, X) = (f(v._1), f(v._2), f(v._3))
}
implicit class AnyTuple4Ops[T](v: (T, T, T, T)) {
def map[X](f: T => X): (X, X, X, X) = (f(v._1), f(v._2), f(v._3), f(v._4))
}
Other operations in similar style can be added, like:
implicit class AnyTuple2Ops[T](v: (T, T)) {
def combine(that: (T, T))(by: (T, T) => T): (T, T) = (by(v._1, that._1), by(v._2, that._2))
}
Would it impair the usability of the library on Scala 3?
I am not sure I understand the question. Do you mean you expect some interference with Scala 3, such that the library would be unusable on some Scala 3 projects because of these tuple operations added?
I'm asking because Scala 3 has its own map method on Tuple, taking a polymorphic function.