diff options
Diffstat (limited to 'src/library')
19 files changed, 81 insertions, 40 deletions
diff --git a/src/library/scala/Option.scala b/src/library/scala/Option.scala index 905e925f57..66900e7258 100644 --- a/src/library/scala/Option.scala +++ b/src/library/scala/Option.scala @@ -211,6 +211,17 @@ sealed abstract class Option[+A] extends Product with Serializable { /** Tests whether the option contains a given value as an element. * + * @example {{{ + * // Returns true because Some instance contains string "something" which equals "something". + * Some("something") contains "something" + * + * // Returns false because "something" != "anything". + * Some("something") contains "anything" + * + * // Returns false when method called on None. + * None contains "anything" + * }}} + * * @param elem the element to test. * @return `true` if the option has an element that is equal (as * determined by `==`) to `elem`, `false` otherwise. @@ -251,6 +262,17 @@ sealed abstract class Option[+A] extends Product with Serializable { * nonempty '''and''' `pf` is defined for that value. * Returns $none otherwise. * + * @example {{{ + * // Returns Some(HTTP) because the partial function covers the case. + * Some("http") collect {case "http" => "HTTP"} + * + * // Returns None because the partial function doesn't cover the case. + * Some("ftp") collect {case "http" => "HTTP"} + * + * // Returns None because None is passed to the collect method. + * None collect {case value => value} + * }}} + * * @param pf the partial function. * @return the result of applying `pf` to this $option's * value (if possible), or $none. diff --git a/src/library/scala/PartialFunction.scala b/src/library/scala/PartialFunction.scala index 7f4a9dc45d..fba759eb32 100644 --- a/src/library/scala/PartialFunction.scala +++ b/src/library/scala/PartialFunction.scala @@ -20,6 +20,11 @@ package scala * {{{ * val f: PartialFunction[Int, Any] = { case _ => 1/0 } * }}} + * + * It is the responsibility of the caller to call `isDefinedAt` before + * calling `apply`, because if `isDefinedAt` is false, it is not guaranteed + * `apply` will throw an exception to indicate an error condition. If an + * exception is not thrown, evaluation may result in an arbitrary value. * * The main distinction between `PartialFunction` and [[scala.Function1]] is * that the user of a `PartialFunction` may choose to do something different diff --git a/src/library/scala/Predef.scala b/src/library/scala/Predef.scala index faeb1dcbe2..7f717aa6e4 100644 --- a/src/library/scala/Predef.scala +++ b/src/library/scala/Predef.scala @@ -303,7 +303,7 @@ object Predef extends LowPriorityImplicits with DeprecatedPredef { @inline implicit def augmentString(x: String): StringOps = new StringOps(x) @inline implicit def unaugmentString(x: StringOps): String = x.repr - // printing and reading ----------------------------------------------- + // printing ----------------------------------------------------------- def print(x: Any) = Console.print(x) def println() = Console.println() diff --git a/src/library/scala/StringContext.scala b/src/library/scala/StringContext.scala index 20a328ec8f..2632994a34 100644 --- a/src/library/scala/StringContext.scala +++ b/src/library/scala/StringContext.scala @@ -38,7 +38,7 @@ import scala.annotation.tailrec * To provide your own string interpolator, create an implicit class * which adds a method to `StringContext`. Here's an example: * {{{ - * implicit class JsonHelper(val sc: StringContext) extends AnyVal { + * implicit class JsonHelper(private val sc: StringContext) extends AnyVal { * def json(args: Any*): JSONObject = ... * } * val x: JSONObject = json"{ a: $a }" diff --git a/src/library/scala/collection/GenMapLike.scala b/src/library/scala/collection/GenMapLike.scala index 4e7d359251..bce9740522 100644 --- a/src/library/scala/collection/GenMapLike.scala +++ b/src/library/scala/collection/GenMapLike.scala @@ -102,7 +102,7 @@ trait GenMapLike[A, +B, +Repr] extends GenIterableLike[(A, B), Repr] with Equals */ def mapValues[C](f: B => C): GenMap[A, C] - /** Compares two maps structurally; i.e. checks if all mappings + /** Compares two maps structurally; i.e., checks if all mappings * contained in this map are also contained in the other map, * and vice versa. * diff --git a/src/library/scala/collection/TraversableLike.scala b/src/library/scala/collection/TraversableLike.scala index d3a7db6968..a8731a51b1 100644 --- a/src/library/scala/collection/TraversableLike.scala +++ b/src/library/scala/collection/TraversableLike.scala @@ -253,7 +253,7 @@ trait TraversableLike[+A, +Repr] extends Any b.result } - private def filterImpl(p: A => Boolean, isFlipped: Boolean): Repr = { + private[scala] def filterImpl(p: A => Boolean, isFlipped: Boolean): Repr = { val b = newBuilder for (x <- this) if (p(x) != isFlipped) b += x diff --git a/src/library/scala/collection/TraversableOnce.scala b/src/library/scala/collection/TraversableOnce.scala index a8c4e047ab..13cd99d910 100644 --- a/src/library/scala/collection/TraversableOnce.scala +++ b/src/library/scala/collection/TraversableOnce.scala @@ -75,7 +75,7 @@ trait TraversableOnce[+A] extends Any with GenTraversableOnce[A] { // at least indirectly. Currently, these are `ArrayOps` and `StringOps`. // It is also implemented in `TraversableOnce[A]`. /** A version of this collection with all - * of the operations implemented sequentially (i.e. in a single-threaded manner). + * of the operations implemented sequentially (i.e., in a single-threaded manner). * * This method returns a reference to this collection. In parallel collections, * it is redefined to return a sequential implementation of this collection. In diff --git a/src/library/scala/collection/immutable/Stream.scala b/src/library/scala/collection/immutable/Stream.scala index d3ff5e8abf..1f97c4c769 100644 --- a/src/library/scala/collection/immutable/Stream.scala +++ b/src/library/scala/collection/immutable/Stream.scala @@ -468,8 +468,18 @@ self => ) else super.flatMap(f)(bf) + override private[scala] def filterImpl(p: A => Boolean, isFlipped: Boolean): Stream[A] = { + // optimization: drop leading prefix of elems for which f returns false + // var rest = this dropWhile (!p(_)) - forget DRY principle - GC can't collect otherwise + var rest = this + while (!rest.isEmpty && p(rest.head) == isFlipped) rest = rest.tail + // private utility func to avoid `this` on stack (would be needed for the lazy arg) + if (rest.nonEmpty) Stream.filteredTail(rest, p, isFlipped) + else Stream.Empty + } + /** Returns all the elements of this `Stream` that satisfy the predicate `p` - * in a new `Stream` - i.e. it is still a lazy data structure. The order of + * in a new `Stream` - i.e., it is still a lazy data structure. The order of * the elements is preserved * * @param p the predicate used to filter the stream. @@ -481,15 +491,7 @@ self => * // produces * }}} */ - override def filter(p: A => Boolean): Stream[A] = { - // optimization: drop leading prefix of elems for which f returns false - // var rest = this dropWhile (!p(_)) - forget DRY principle - GC can't collect otherwise - var rest = this - while (!rest.isEmpty && !p(rest.head)) rest = rest.tail - // private utility func to avoid `this` on stack (would be needed for the lazy arg) - if (rest.nonEmpty) Stream.filteredTail(rest, p) - else Stream.Empty - } + override def filter(p: A => Boolean): Stream[A] = filterImpl(p, isFlipped = false) // This override is only left in 2.11 because of binary compatibility, see PR #3925 override final def withFilter(p: A => Boolean): StreamWithFilter = new StreamWithFilter(p) @@ -1187,8 +1189,8 @@ object Stream extends SeqFactory[Stream] { else cons(start, range(start + step, end, step)) } - private[immutable] def filteredTail[A](stream: Stream[A], p: A => Boolean) = { - cons(stream.head, stream.tail filter p) + private[immutable] def filteredTail[A](stream: Stream[A], p: A => Boolean, isFlipped: Boolean) = { + cons(stream.head, stream.tail.filterImpl(p, isFlipped)) } private[immutable] def collectedTail[A, B, That](head: B, stream: Stream[A], pf: PartialFunction[A, B], bf: CanBuildFrom[Stream[A], B, That]) = { diff --git a/src/library/scala/collection/immutable/StringLike.scala b/src/library/scala/collection/immutable/StringLike.scala index 8e1d950d00..738b294ce6 100644 --- a/src/library/scala/collection/immutable/StringLike.scala +++ b/src/library/scala/collection/immutable/StringLike.scala @@ -121,14 +121,14 @@ self => } /** Return all lines in this string in an iterator, excluding trailing line - * end characters, i.e. apply `.stripLineEnd` to all lines + * end characters, i.e., apply `.stripLineEnd` to all lines * returned by `linesWithSeparators`. */ def lines: Iterator[String] = linesWithSeparators map (line => new WrappedString(line).stripLineEnd) /** Return all lines in this string in an iterator, excluding trailing line - * end characters, i.e. apply `.stripLineEnd` to all lines + * end characters, i.e., apply `.stripLineEnd` to all lines * returned by `linesWithSeparators`. */ @deprecated("Use `lines` instead.","2.11.0") diff --git a/src/library/scala/concurrent/Future.scala b/src/library/scala/concurrent/Future.scala index 4ed0687334..e93a3284dc 100644 --- a/src/library/scala/concurrent/Future.scala +++ b/src/library/scala/concurrent/Future.scala @@ -102,7 +102,7 @@ trait Future[+T] extends Awaitable[T] { /* Callbacks */ - /** When this future is completed successfully (i.e. with a value), + /** When this future is completed successfully (i.e., with a value), * apply the provided partial function to the value if the partial function * is defined at that value. * @@ -118,7 +118,7 @@ trait Future[+T] extends Awaitable[T] { case _ => } - /** When this future is completed with a failure (i.e. with a throwable), + /** When this future is completed with a failure (i.e., with a throwable), * apply the provided callback to the throwable. * * $caughtThrowables diff --git a/src/library/scala/concurrent/duration/Deadline.scala b/src/library/scala/concurrent/duration/Deadline.scala index 61cbe47530..a25a478602 100644 --- a/src/library/scala/concurrent/duration/Deadline.scala +++ b/src/library/scala/concurrent/duration/Deadline.scala @@ -25,15 +25,15 @@ package scala.concurrent.duration */ case class Deadline private (time: FiniteDuration) extends Ordered[Deadline] { /** - * Return a deadline advanced (i.e. moved into the future) by the given duration. + * Return a deadline advanced (i.e., moved into the future) by the given duration. */ def +(other: FiniteDuration): Deadline = copy(time = time + other) /** - * Return a deadline moved backwards (i.e. towards the past) by the given duration. + * Return a deadline moved backwards (i.e., towards the past) by the given duration. */ def -(other: FiniteDuration): Deadline = copy(time = time - other) /** - * Calculate time difference between this and the other deadline, where the result is directed (i.e. may be negative). + * Calculate time difference between this and the other deadline, where the result is directed (i.e., may be negative). */ def -(other: Deadline): FiniteDuration = time - other.time /** diff --git a/src/library/scala/math/BigDecimal.scala b/src/library/scala/math/BigDecimal.scala index bcbed645a7..5a81710986 100644 --- a/src/library/scala/math/BigDecimal.scala +++ b/src/library/scala/math/BigDecimal.scala @@ -617,10 +617,10 @@ extends ScalaNumber with ScalaNumericConversions with Serializable { */ def abs: BigDecimal = if (signum < 0) unary_- else this - /** Returns the sign of this BigDecimal, i.e. + /** Returns the sign of this BigDecimal; * -1 if it is less than 0, - * +1 if it is greater than 0 - * 0 if it is equal to 0 + * +1 if it is greater than 0, + * 0 if it is equal to 0. */ def signum: Int = this.bigDecimal.signum() diff --git a/src/library/scala/math/BigInt.scala b/src/library/scala/math/BigInt.scala index 689fc0c3e1..abc7371d9f 100644 --- a/src/library/scala/math/BigInt.scala +++ b/src/library/scala/math/BigInt.scala @@ -282,10 +282,10 @@ final class BigInt(val bigInteger: BigInteger) extends ScalaNumber with ScalaNum */ def abs: BigInt = new BigInt(this.bigInteger.abs()) - /** Returns the sign of this BigInt, i.e. + /** Returns the sign of this BigInt; * -1 if it is less than 0, - * +1 if it is greater than 0 - * 0 if it is equal to 0 + * +1 if it is greater than 0, + * 0 if it is equal to 0. */ def signum: Int = this.bigInteger.signum() diff --git a/src/library/scala/math/Ordering.scala b/src/library/scala/math/Ordering.scala index d1a4e7c35c..0d7ea8bce2 100644 --- a/src/library/scala/math/Ordering.scala +++ b/src/library/scala/math/Ordering.scala @@ -26,7 +26,7 @@ import scala.language.{implicitConversions, higherKinds} * val pairs = Array(("a", 5, 2), ("c", 3, 1), ("b", 1, 3)) * * // sort by 2nd element - * Sorting.quickSort(pairs)(Ordering.by[(String, Int, Int), Int](_._2) + * Sorting.quickSort(pairs)(Ordering.by[(String, Int, Int), Int](_._2)) * * // sort by the 3rd element, then 1st * Sorting.quickSort(pairs)(Ordering[(Int, String)].on(x => (x._3, x._1))) diff --git a/src/library/scala/math/PartialOrdering.scala b/src/library/scala/math/PartialOrdering.scala index 9e35381528..8d7fc32535 100644 --- a/src/library/scala/math/PartialOrdering.scala +++ b/src/library/scala/math/PartialOrdering.scala @@ -15,17 +15,24 @@ package math * latter. * * A [[http://en.wikipedia.org/wiki/Partial_order partial ordering]] is a - * binary relation on a type `T` that is also an equivalence relation on - * values of type `T`. This relation is exposed as the `lteq` method of - * the `PartialOrdering` trait. This relation must be: + * binary relation on a type `T`, exposed as the `lteq` method of this trait. + * This relation must be: * * - reflexive: `lteq(x, x) == '''true'''`, for any `x` of type `T`. - * - anti-symmetric: `lteq(x, y) == '''true'''` and `lteq(y, x) == true` - * then `equiv(x, y)`, for any `x` and `y` of type `T`. + * - anti-symmetric: if `lteq(x, y) == '''true'''` and + * `lteq(y, x) == '''true'''` + * then `equiv(x, y) == '''true'''`, for any `x` and `y` of type `T`. * - transitive: if `lteq(x, y) == '''true'''` and * `lteq(y, z) == '''true'''` then `lteq(x, z) == '''true'''`, * for any `x`, `y`, and `z` of type `T`. * + * Additionally, a partial ordering induces an + * [[http://en.wikipedia.org/wiki/Equivalence_relation equivalence relation]] + * on a type `T`: `x` and `y` of type `T` are equivalent if and only if + * `lteq(x, y) && lteq(y, x) == '''true'''`. This equivalence relation is + * exposed as the `equiv` method, inherited from the + * [[scala.math.Equiv Equiv]] trait. + * * @author Geoffrey Washburn * @version 1.0, 2008-04-0-3 * @since 2.7 diff --git a/src/library/scala/runtime/ScalaRunTime.scala b/src/library/scala/runtime/ScalaRunTime.scala index 5fb24f2a36..f50059ce54 100644 --- a/src/library/scala/runtime/ScalaRunTime.scala +++ b/src/library/scala/runtime/ScalaRunTime.scala @@ -62,7 +62,7 @@ object ScalaRunTime { } /** Return the class object representing an unboxed value type, - * e.g. classOf[int], not classOf[java.lang.Integer]. The compiler + * e.g., classOf[int], not classOf[java.lang.Integer]. The compiler * rewrites expressions like 5.getClass to come here. */ def anyValClass[T <: AnyVal : ClassTag](value: T): jClass[T] = diff --git a/src/library/scala/runtime/SeqCharSequence.scala b/src/library/scala/runtime/SeqCharSequence.scala index ce7d7afc9e..74e67bb9e7 100644 --- a/src/library/scala/runtime/SeqCharSequence.scala +++ b/src/library/scala/runtime/SeqCharSequence.scala @@ -44,5 +44,10 @@ final class ArrayCharSequence(val xs: Array[Char], start: Int, end: Int) extends new ArrayCharSequence(xs, start1, start1 + newlen) } } - override def toString = xs drop start take length mkString "" + override def toString = { + val start = math.max(this.start, 0) + val end = math.min(xs.length, start + length) + + if (start >= end) "" else new String(xs, start, end - start) + } } diff --git a/src/library/scala/sys/Prop.scala b/src/library/scala/sys/Prop.scala index 04c7b5108c..17ae8cb69c 100644 --- a/src/library/scala/sys/Prop.scala +++ b/src/library/scala/sys/Prop.scala @@ -20,7 +20,7 @@ package sys * @since 2.9 */ trait Prop[+T] { - /** The full name of the property, e.g. "java.awt.headless". + /** The full name of the property, e.g., "java.awt.headless". */ def key: String diff --git a/src/library/scala/util/Properties.scala b/src/library/scala/util/Properties.scala index 2daa4de9a6..8835730d95 100644 --- a/src/library/scala/util/Properties.scala +++ b/src/library/scala/util/Properties.scala @@ -107,7 +107,7 @@ private[scala] trait PropertiesTrait { val versionString = "version " + scalaPropOrElse("version.number", "(unknown)") val copyrightString = scalaPropOrElse("copyright.string", "Copyright 2002-2013, LAMP/EPFL") - /** This is the encoding to use reading in source files, overridden with -encoding + /** This is the encoding to use reading in source files, overridden with -encoding. * Note that it uses "prop" i.e. looks in the scala jar, not the system properties. */ def sourceEncoding = scalaPropOrElse("file.encoding", "UTF-8") |