Skip to content

Commit

Permalink
Revert "Moved ancillary methods off specialized traits."
Browse files Browse the repository at this point in the history
This reverts commit 1d0372f.

I forgot about polymorphic dispatch.  Have to seek another way.
  • Loading branch information
paulp committed Apr 27, 2012
1 parent 57574bb commit 1c3f66b
Show file tree
Hide file tree
Showing 15 changed files with 294 additions and 184 deletions.
132 changes: 125 additions & 7 deletions src/build/genprod.scala
Expand Up @@ -123,7 +123,23 @@ object FunctionOne extends Function(1) {
* assert(succ(0) == anonfun1(0))
* """)

override def moreMethods = ""
override def moreMethods = """
/** Composes two instances of Function1 in a new Function1, with this function applied last.
*
* @tparam A the type to which function `g` can be applied
* @param g a function A => T1
* @return a new function `f` such that `f(x) == apply(g(x))`
*/
def compose[A](g: A => T1): A => R = { x => apply(g(x)) }
/** Composes two instances of Function1 in a new Function1, with this function applied first.
*
* @tparam A the result type of function `g`
* @param g a function R => A
* @return a new function `f` such that `f(x) == g(apply(x))`
*/
def andThen[A](g: R => A): T1 => A = { x => g(apply(x)) }
"""
}

object FunctionTwo extends Function(2) {
Expand All @@ -139,8 +155,6 @@ object FunctionTwo extends Function(2) {
* }
* assert(max(0, 1) == anonfun2(0, 1))
* """)

override def moreMethods = ""
}

object Function {
Expand Down Expand Up @@ -241,6 +255,11 @@ class Function(val i: Int) extends Group("Function") with Arity {
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz */

object Tuple {
val zipImports = """
import scala.collection.{ TraversableLike => TLike, IterableLike => ILike }
import scala.collection.generic.{ CanBuildFrom => CBF }
"""

def make(i: Int) = apply(i)()
def apply(i: Int) = i match {
case 1 => TupleOne
Expand All @@ -257,21 +276,120 @@ object TupleOne extends Tuple(1)

object TupleTwo extends Tuple(2)
{
override def imports = Tuple.zipImports
override def covariantSpecs = "@specialized(Int, Long, Double, Char, Boolean, AnyRef) "
override def moreMethods = """
/** Swaps the elements of this `Tuple`.
* @return a new Tuple where the first element is the second element of this Tuple and the
* second element is the first element of this Tuple.
*/
def swap: Tuple2[T2,T1] = Tuple2(_2, _1)
@deprecated("Use `zipped` instead.", "2.9.0")
def zip[Repr1, El1, El2, To](implicit w1: T1 => TLike[El1, Repr1],
w2: T2 => Iterable[El2],
cbf1: CBF[Repr1, (El1, El2), To]): To = {
zipped map ((x, y) => ((x, y)))
}
/** Wraps a tuple in a `Zipped`, which supports 2-ary generalisations of `map`, `flatMap`, `filter`, etc.
* Note that there must be an implicit value to convert this tuple's types into a [[scala.collection.TraversableLike]]
* or [[scala.collection.IterableLike]].
* {{{
* scala> val tuple = (List(1,2,3),List('a','b','c'))
* tuple: (List[Int], List[Char]) = (List(1, 2, 3),List(a, b, c))
*
* scala> tuple.zipped map { (x,y) => x + ":" + y }
* res6: List[java.lang.String] = List(1:a, 2:b, 3:c)
* }}}
*
* @see Zipped
* Note: will not terminate for infinite-sized collections.
*/
def zipped[Repr1, El1, Repr2, El2](implicit w1: T1 => TLike[El1, Repr1], w2: T2 => ILike[El2, Repr2]): Zipped[Repr1, El1, Repr2, El2]
= new Zipped[Repr1, El1, Repr2, El2](_1, _2)
class Zipped[+Repr1, +El1, +Repr2, +El2](coll1: TLike[El1, Repr1], coll2: ILike[El2, Repr2]) { // coll2: ILike for filter
def map[B, To](f: (El1, El2) => B)(implicit cbf: CBF[Repr1, B, To]): To = {
val b = cbf(coll1.repr)
b.sizeHint(coll1)
val elems2 = coll2.iterator
for (el1 <- coll1) {
if (elems2.hasNext)
b += f(el1, elems2.next)
else
return b.result
}
b.result
}
def flatMap[B, To](f: (El1, El2) => TraversableOnce[B])(implicit cbf: CBF[Repr1, B, To]): To = {
val b = cbf(coll1.repr)
val elems2 = coll2.iterator
for (el1 <- coll1) {
if (elems2.hasNext)
b ++= f(el1, elems2.next)
else
return b.result
}
b.result
}
def filter[To1, To2](f: (El1, El2) => Boolean)(implicit cbf1: CBF[Repr1, El1, To1], cbf2: CBF[Repr2, El2, To2]): (To1, To2) = {
val b1 = cbf1(coll1.repr)
val b2 = cbf2(coll2.repr)
val elems2 = coll2.iterator
for (el1 <- coll1) {
if (elems2.hasNext) {
val el2 = elems2.next
if (f(el1, el2)) {
b1 += el1
b2 += el2
}
}
else return (b1.result, b2.result)
}
(b1.result, b2.result)
}
def exists(f: (El1, El2) => Boolean): Boolean = {
val elems2 = coll2.iterator
for (el1 <- coll1) {
if (elems2.hasNext) {
if (f(el1, elems2.next))
return true
}
else return false
}
false
}
def forall(f: (El1, El2) => Boolean): Boolean =
!exists((x, y) => !f(x, y))
def foreach[U](f: (El1, El2) => U): Unit = {
val elems2 = coll2.iterator
for (el1 <- coll1) {
if (elems2.hasNext)
f(el1, elems2.next)
else
return
}
}
}
"""
}

object TupleThree extends Tuple(3) {
override def imports = """
import scala.collection.{ TraversableLike => TLike, IterableLike => ILike }
import scala.collection.generic.{ CanBuildFrom => CBF }
"""
override def imports = Tuple.zipImports
override def moreMethods = """
@deprecated("Use `zipped` instead.", "2.9.0")
Expand Down
1 change: 0 additions & 1 deletion src/compiler/scala/tools/nsc/backend/jvm/GenJVM.scala
Expand Up @@ -1324,7 +1324,6 @@ abstract class GenJVM extends SubComponent with GenJVMUtil with GenAndroid with
val lastInstr = b.lastInstruction

for (instr <- b) {

instr match {
case THIS(clasz) => jcode.emitALOAD_0()

Expand Down
2 changes: 1 addition & 1 deletion src/library/scala/Function0.scala
Expand Up @@ -6,7 +6,7 @@
** |/ **
\* */
// GENERATED CODE: DO NOT EDIT.
// genprod generated these sources at: Tue Apr 24 16:32:13 PDT 2012
// genprod generated these sources at: Tue Feb 14 16:49:03 PST 2012

package scala

Expand Down
16 changes: 16 additions & 0 deletions src/library/scala/Function1.scala
Expand Up @@ -38,5 +38,21 @@ trait Function1[@specialized(scala.Int, scala.Long, scala.Float, scala.Double, s
*/
def apply(v1: T1): R

/** Composes two instances of Function1 in a new Function1, with this function applied last.
*
* @tparam A the type to which function `g` can be applied
* @param g a function A => T1
* @return a new function `f` such that `f(x) == apply(g(x))`
*/
def compose[A](g: A => T1): A => R = { x => apply(g(x)) }

/** Composes two instances of Function1 in a new Function1, with this function applied first.
*
* @tparam A the result type of function `g`
* @param g a function R => A
* @return a new function `f` such that `f(x) == g(apply(x))`
*/
def andThen[A](g: R => A): T1 => A = { x => g(apply(x)) }

override def toString() = "<function1>"
}
14 changes: 14 additions & 0 deletions src/library/scala/Function2.scala
Expand Up @@ -37,6 +37,20 @@ trait Function2[@specialized(scala.Int, scala.Long, scala.Double) -T1, @speciali
* @return the result of function application.
*/
def apply(v1: T1, v2: T2): R
/** Creates a curried version of this function.
*
* @return a function `f` such that `f(x1)(x2) == apply(x1, x2)`
*/ def curried: T1 => T2 => R = {
(x1: T1) => (x2: T2) => apply(x1, x2)
}

/** Creates a tupled version of this function: instead of 2 arguments,
* it accepts a single [[scala.Tuple2]] argument.
*
* @return a function `f` such that `f((x1, x2)) == f(Tuple2(x1, x2)) == apply(x1, x2)`
*/
def tupled: Tuple2[T1, T2] => R = {
case Tuple2(x1, x2) => apply(x1, x2)
}
override def toString() = "<function2>"
}
2 changes: 1 addition & 1 deletion src/library/scala/PartialFunction.scala
Expand Up @@ -78,7 +78,7 @@ trait PartialFunction[-A, +B] extends (A => B) { self =>
* @return a partial function with the same domain as this partial function, which maps
* arguments `x` to `k(this(x))`.
*/
def andThen[C](k: B => C) : PartialFunction[A, C] = new PartialFunction[A, C] {
override def andThen[C](k: B => C) : PartialFunction[A, C] = new PartialFunction[A, C] {
def isDefinedAt(x: A): Boolean = self isDefinedAt x
def apply(x: A): C = k(self(x))
}
Expand Down
8 changes: 4 additions & 4 deletions src/library/scala/Predef.scala
Expand Up @@ -308,11 +308,11 @@ object Predef extends LowPriorityImplicits {
// views --------------------------------------------------------------

implicit def exceptionWrapper(exc: Throwable) = new runtime.RichException(exc)
implicit def function1ToRichFunction1[T, R](f: T => R): runtime.RichFunction1[T, R] = new runtime.RichFunction1(f)
implicit def function2ToRichFunction2[T1, T2, R](f: (T1, T2) => R): runtime.RichFunction2[T1, T2, R] =
new runtime.RichFunction2(f)

implicit def tuple2ToZipped[T1, T2](zz: (T1, T2)) = new runtime.Tuple2ZippedOps(zz)
implicit def zipped2ToTraversable[El1, El2](zz: Tuple2[_, _]#Zipped[_, El1, _, El2]): Traversable[(El1, El2)] =
new collection.AbstractTraversable[(El1, El2)] {
def foreach[U](f: ((El1, El2)) => U): Unit = zz foreach Function.untupled(f)
}

implicit def zipped3ToTraversable[El1, El2, El3](zz: Tuple3[_, _, _]#Zipped[_, El1, _, El2, _, El3]): Traversable[(El1, El2, El3)] =
new collection.AbstractTraversable[(El1, El2, El3)] {
Expand Down
114 changes: 114 additions & 0 deletions src/library/scala/Tuple2.scala
Expand Up @@ -9,6 +9,9 @@

package scala

import scala.collection.{ TraversableLike => TLike, IterableLike => ILike }
import scala.collection.generic.{ CanBuildFrom => CBF }


/** A tuple of 2 elements; the canonical representation of a [[scala.Product2]].
*
Expand All @@ -27,4 +30,115 @@ case class Tuple2[@specialized(Int, Long, Double, Char, Boolean, AnyRef) +T1, @s
*/
def swap: Tuple2[T2,T1] = Tuple2(_2, _1)

@deprecated("Use `zipped` instead.", "2.9.0")
def zip[Repr1, El1, El2, To](implicit w1: T1 => TLike[El1, Repr1],
w2: T2 => Iterable[El2],
cbf1: CBF[Repr1, (El1, El2), To]): To = {
zipped map ((x, y) => ((x, y)))
}

/** Wraps a tuple in a `Zipped`, which supports 2-ary generalisations of `map`, `flatMap`, `filter`, etc.
* Note that there must be an implicit value to convert this tuple's types into a [[scala.collection.TraversableLike]]
* or [[scala.collection.IterableLike]].
* {{{
* scala> val tuple = (List(1,2,3),List('a','b','c'))
* tuple: (List[Int], List[Char]) = (List(1, 2, 3),List(a, b, c))
*
* scala> tuple.zipped map { (x,y) => x + ":" + y }
* res6: List[java.lang.String] = List(1:a, 2:b, 3:c)
* }}}
*
* @see Zipped
* Note: will not terminate for infinite-sized collections.
*/
def zipped[Repr1, El1, Repr2, El2](implicit w1: T1 => TLike[El1, Repr1], w2: T2 => ILike[El2, Repr2]): Zipped[Repr1, El1, Repr2, El2]
= new Zipped[Repr1, El1, Repr2, El2](_1, _2)

/**
* @define coll zipped
* @define Coll Zipped
* @define orderDependent
* @define orderDependentFold
* @define mayNotTerminateInf
* @define willNotTerminateInf
* @define collectExample
* @define undefinedorder
*/
class Zipped[+Repr1, +El1, +Repr2, +El2](coll1: TLike[El1, Repr1], coll2: ILike[El2, Repr2]) { // coll2: ILike for filter
def map[B, To](f: (El1, El2) => B)(implicit cbf: CBF[Repr1, B, To]): To = {
val b = cbf(coll1.repr)
b.sizeHint(coll1)
val elems2 = coll2.iterator

for (el1 <- coll1) {
if (elems2.hasNext)
b += f(el1, elems2.next)
else
return b.result
}

b.result
}

def flatMap[B, To](f: (El1, El2) => TraversableOnce[B])(implicit cbf: CBF[Repr1, B, To]): To = {
val b = cbf(coll1.repr)
val elems2 = coll2.iterator

for (el1 <- coll1) {
if (elems2.hasNext)
b ++= f(el1, elems2.next)
else
return b.result
}

b.result
}

def filter[To1, To2](f: (El1, El2) => Boolean)(implicit cbf1: CBF[Repr1, El1, To1], cbf2: CBF[Repr2, El2, To2]): (To1, To2) = {
val b1 = cbf1(coll1.repr)
val b2 = cbf2(coll2.repr)
val elems2 = coll2.iterator

for (el1 <- coll1) {
if (elems2.hasNext) {
val el2 = elems2.next
if (f(el1, el2)) {
b1 += el1
b2 += el2
}
}
else return (b1.result, b2.result)
}

(b1.result, b2.result)
}

def exists(f: (El1, El2) => Boolean): Boolean = {
val elems2 = coll2.iterator

for (el1 <- coll1) {
if (elems2.hasNext) {
if (f(el1, elems2.next))
return true
}
else return false
}
false
}

def forall(f: (El1, El2) => Boolean): Boolean =
!exists((x, y) => !f(x, y))

def foreach[U](f: (El1, El2) => U): Unit = {
val elems2 = coll2.iterator

for (el1 <- coll1) {
if (elems2.hasNext)
f(el1, elems2.next)
else
return
}
}
}

}
11 changes: 11 additions & 0 deletions src/library/scala/Tuple3.scala
Expand Up @@ -53,6 +53,17 @@ case class Tuple3[+T1, +T2, +T3](_1: T1, _2: T2, _3: T3)
w3: T3 => ILike[El3, Repr3]): Zipped[Repr1, El1, Repr2, El2, Repr3, El3]
= new Zipped[Repr1, El1, Repr2, El2, Repr3, El3](_1, _2, _3)

/**
* @define coll zipped
* @define Coll Zipped
* @define orderDependent
* @define orderDependentFold
* @define mayNotTerminateInf
* @define willNotTerminateInf
* @define collectExample
* @define undefinedorder
* @define thatInfo The class of the returned collection.
*/
class Zipped[+Repr1, +El1, +Repr2, +El2, +Repr3, +El3](coll1: TLike[El1, Repr1],
coll2: ILike[El2, Repr2],
coll3: ILike[El3, Repr3]) {
Expand Down

0 comments on commit 1c3f66b

Please sign in to comment.