I want following function
range((1,1), (2,2)) which return
Seq[(Int,Int)]((1,1),(1,2),(2,1),(2,2))
It is analog for one dimensional range with 1 to 2
The function should work for any scala tuple (i.e. Tuple2, Tuple3, Tuple4, …) and be typesafe.
I’ve tried with
def tupleRange[T <: Product](t1:T, t2:T):Seq[T] = {
assert(t1.productArity == t2.productArity)
def tail(t:Product):Product = sys.error("todo");
def join(i:Int, p:Product):T = sys.error("todo");
for(
v <- t1.productElement(0).asInstanceOf[Int] to t2.productElement(0).asInstanceOf[Int];
v2 <- tupleRange(tail(t1), tail(t2)))
yield join(v,v2)
}
implicit def range[T <:Product](p1:T) = new { def to(p2:T) = tupleRange(p1,p2)}
But I think I’ve chosen wrong direction.
I’d suggest the same thing that @ziggystar suggested above. Use
List[Int]instead of tuples ofInts.This implementation assumes that
xsandysare ordered and have same length.