scala-collection-contrib icon indicating copy to clipboard operation
scala-collection-contrib copied to clipboard

Collection-like operations on tuples

Open OndrejSpanel opened this issue 2 years ago • 3 comments
trafficstars

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))
  }

OndrejSpanel avatar Jan 20 '23 11:01 OndrejSpanel

Would it impair the usability of the library on Scala 3?

SethTisue avatar Mar 04 '23 23:03 SethTisue

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?

OndrejSpanel avatar Mar 05 '23 08:03 OndrejSpanel

I'm asking because Scala 3 has its own map method on Tuple, taking a polymorphic function.

SethTisue avatar Mar 07 '23 21:03 SethTisue