Skip to content

Collection-like operations on tuples #199

Description

@OndrejSpanel

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions