Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A proposal to enhance Tuple2#{zip,zipped} #3526

Closed
scabug opened this issue Jun 3, 2010 · 5 comments
Closed

A proposal to enhance Tuple2#{zip,zipped} #3526

scabug opened this issue Jun 3, 2010 · 5 comments
Assignees

Comments

@scabug
Copy link

scabug commented Jun 3, 2010

Since #3511 has been fixed, I have a proposal to enhance the methods zip&zipped in Tupel2(Tuple3). As we known, we can use (xs,ys).zip and (xs,ys).zipped in Scala2.8 where xs and ys are both some type of Traversable. There are some limited in current implementation.
Let

xs = List.range(0,10)
ys = Stream.from(0)

,then the following codes are invalid:

(ys,xs).zip
(ys,ys).zip

While I expected:

(ys,xs).zip == Stream((0,0),..,(9,9))
(ys,ys).zip == Stream((0,0),..)

Here is my proposal which could fix these shortages:

class PairX[+T1,+T2](t: (T1,T2)) {
  import scala.collection.{TraversableLike,TraversableView}
  import scala.collection.generic.CanBuildFrom
  import t._
  def zipx[Repr1, El1, El2, To](implicit w1:   T1 => TraversableLike[El1, Repr1],
                                        w2:   T2 => Iterable[El2],
                                        cbf1: CanBuildFrom[Repr1, (El1, El2), To]): To = {
    val coll1: TraversableLike[El1, Repr1] = _1
    val coll2: Iterable[El2] = _2
    val elems2 = coll2.iterator

    coll1.view.takeWhile{ _ => elems2.hasNext }.map{ el1 => (el1,elems2.next) }.asInstanceOf[TraversableView[(El1,El2),Repr1]].force
  }
}

implicit def p2px[T1,T2](t: (T1,T2)): PairX[T1,T2] = new PairX(t)  

Similar changes should be made in Tuple2#Zipped#{map,flatMap,filter}, Tuple3#zip and Tuple3#Zipped#{map,flatMap,filter}.
The effection:

scala> val xs = List.range(0,10)
xs: List[Int] = List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

scala> val ys = Stream.from(0)
ys: scala.collection.immutable.Stream[Int] = Stream(0, ?)

scala> (ys,xs).zipx
res2: scala.collection.immutable.Stream[(Int, Int)] = Stream((0,0), ?)

scala> res2.force
res3: scala.collection.immutable.Stream[(Int, Int)] = Stream((0,0), (1,1), (2,2), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9))

Note: I think that this proposal doesn't fix #2634, since there is some problems of Stream#iterator(see #3273).

@scabug
Copy link
Author

scabug commented Jun 3, 2010

Imported From: https://issues.scala-lang.org/browse/SI-3526?orig=1
Reporter: Eastsun (eastsun)

@scabug
Copy link
Author

scabug commented Jun 7, 2010

@dragos said:
Adriaan, I think you implemented zipp[ed], can you please have a look? Feel free to reassign if I'm wrong.

@scabug
Copy link
Author

scabug commented Sep 16, 2010

@paulp said:
I'm glad you brought this to my attention. There are numerous issues in there I was unaware of. Working on it now.

@scabug
Copy link
Author

scabug commented Sep 20, 2010

@paulp said:
See proposed alterations in http://permalink.gmane.org/gmane.comp.lang.scala.internals/3890 , awaiting opinion from adriaan.

@scabug
Copy link
Author

scabug commented Oct 27, 2010

@paulp said:
(In r23384) Some issues with Tuple2/3. There was a stray wrong-arity
foreach method in Tuple3, and both classes have what is now
a redundant zip method which is also unfortunately completely
strict in a not entirely fixable fashion. So "zip" is
deprecated in favor of zipped. Closes #3526, but the code
which closes that is primarily found in r23228. No review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants