Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions library/src/scala/collection/ArrayOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ final class ArrayOps[A](private val xs: Array[A]) extends AnyVal {
* @param f the 'split function' mapping the elements of this array to an [[scala.util.Either]]
*
* @return a pair of arrays: the first one made of those values returned by `f` that were wrapped in [[scala.util.Left]],
* and the second one made of those wrapped in [[scala.util.Right]].
* and the second one made of those wrapped in [[scala.util.Right]].
*/
def partitionMap[A1: ClassTag, A2: ClassTag](f: A => Either[A1, A2]): (Array[A1], Array[A2]) = {
val res1 = ArrayBuilder.make[A1]
Expand Down Expand Up @@ -1032,7 +1032,7 @@ final class ArrayOps[A](private val xs: Array[A]) extends AnyVal {
}

/** Finds the first element of the array for which the given partial function is defined, and applies the
* partial function to it.
* partial function to it.
*/
def collectFirst[B](@deprecatedName("f","2.13.9") pf: PartialFunction[A, B]^): Option[B] = {
val fallback: Any => Any = ArrayOps.fallback
Expand Down Expand Up @@ -1623,7 +1623,7 @@ final class ArrayOps[A](private val xs: Array[A]) extends AnyVal {
* the order of each `x` element is also arbitrary.
*
* @return An Iterator which traverses the n-element combinations of this array
* @example ```
* @example ```scala sc:compile
* Array('a', 'b', 'b', 'b', 'c').combinations(2).map(runtime.ScalaRunTime.stringOf).foreach(println)
* // Array(a, b)
* // Array(a, c)
Expand Down
83 changes: 34 additions & 49 deletions library/src/scala/collection/IterableOnce.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ import IterableOnce.elemsToCopyToArray
*
* Note: `IterableOnce` does not extend [[IterableOnceOps]]. This is different than the general
* design of the collections library, which uses the following pattern:
* ```
* trait Seq extends Iterable with SeqOps
* trait SeqOps extends IterableOps
* ```scala sc:compile
* transparent trait SeqOps[+A, +CC[_], +C] extends Any
* trait Seq[+A] extends Iterable[A] with SeqOps[A, Seq, Seq[A]]
*
* trait IndexedSeq extends Seq with IndexedSeqOps
* trait IndexedSeqOps extends SeqOps
* transparent trait IndexedSeqOps[+A, +CC[_], +C] extends Any with SeqOps[A, CC, C]
* trait IndexedSeq[+A] extends Seq[A] with IndexedSeqOps[A, IndexedSeq, IndexedSeq[A]]
* ```
*
* The goal is to provide a minimal interface without any sequential operations. This allows
Expand Down Expand Up @@ -383,12 +383,9 @@ transparent trait IterableOnceOps[+A, +CC[_], +C] extends Any { this: IterableOn
*
* Example:
*
* ```
* scala> List(1, 2, 3, 100, 4).takeWhile(n => n < 10)
* val res0: List[Int] = List(1, 2, 3)
*
* scala> List(1, 2, 3, 100, 4).takeWhile(n => n == 0)
* val res1: List[Int] = List()
* ```scala sc:compile
* List(1, 2, 3, 100, 4).takeWhile(n => n < 10) // List(1, 2, 3)
* List(1, 2, 3, 100, 4).takeWhile(n => n == 0) // List()
* ```
*
* Use [[span]] to obtain both the prefix and suffix.
Expand Down Expand Up @@ -418,12 +415,9 @@ transparent trait IterableOnceOps[+A, +CC[_], +C] extends Any { this: IterableOn
*
* Example:
*
* ```
* scala> List(1, 2, 3, 100, 4).dropWhile(n => n < 10)
* val res0: List[Int] = List(100, 4)
*
* scala> List(1, 2, 3, 100, 4).dropWhile(n => n == 0)
* val res1: List[Int] = List(1, 2, 3, 100, 4)
* ```scala sc:compile
* List(1, 2, 3, 100, 4).dropWhile(n => n < 10) // List(100, 4)
* List(1, 2, 3, 100, 4).dropWhile(n => n == 0) // List(1, 2, 3, 100, 4)
* ```
*
* Use [[span]] to obtain both the prefix and suffix.
Expand Down Expand Up @@ -467,19 +461,19 @@ transparent trait IterableOnceOps[+A, +CC[_], +C] extends Any { this: IterableOn
*
* For example:
*
* ```
* ```scala sc:compile
* def getWords(lines: Seq[String]): Seq[String] = lines.flatMap(line => line.split("\\W+"))
* ```
*
* The type of the resulting collection is guided by the static type of this $coll. This might
* cause unexpected results sometimes. For example:
*
* ```
* ```scala sc:compile
* // lettersOf will return a Seq[Char] of likely repeated letters, instead of a Set
* def lettersOf(words: Seq[String]) = words.flatMap(word => word.toSet)
*
* // lettersOf will return a Set[Char], not a Seq
* def lettersOf(words: Seq[String]) = words.toSet.flatMap(word => word.toSeq)
* // lettersOf2 will return a Set[Char], not a Seq
* def lettersOf2(words: Seq[String]) = words.toSet.flatMap(word => word.toSeq)
*
* // xs will be an Iterable[Int]
* val xs = Map("a" -> List(11, 111), "b" -> List(22, 222)).flatMap(_._2)
Expand All @@ -501,7 +495,7 @@ transparent trait IterableOnceOps[+A, +CC[_], +C] extends Any { this: IterableOn
* The resulting collection's type will be guided by the
* type of $coll. For example:
*
* ```
* ```scala sc:compile
* val xs = List(
* Set(1, 2, 3),
* Set(1, 2, 3)
Expand Down Expand Up @@ -1372,15 +1366,10 @@ transparent trait IterableOnceOps[+A, +CC[_], +C] extends Any { this: IterableOn
*
* Example:
*
* ```
* scala> val a = List(1,2,3,4)
* a: List[Int] = List(1, 2, 3, 4)
*
* scala> val b = new StringBuilder()
* b: StringBuilder =
*
* scala> a.addString(b , "List(" , ", " , ")")
* res5: StringBuilder = List(1, 2, 3, 4)
* ```scala sc:compile
* val a = List(1,2,3,4) // List(1, 2, 3, 4)
* val b = new StringBuilder()
* a.addString(b , "List(" , ", " , ")") // List(1, 2, 3, 4)
* ```
*
* @param b the string builder to which elements are appended.
Expand Down Expand Up @@ -1410,15 +1399,10 @@ transparent trait IterableOnceOps[+A, +CC[_], +C] extends Any { this: IterableOn
*
* Example:
*
* ```
* scala> val a = List(1,2,3,4)
* a: List[Int] = List(1, 2, 3, 4)
*
* scala> val b = new StringBuilder()
* b: StringBuilder =
*
* scala> a.addString(b, ", ")
* res0: StringBuilder = 1, 2, 3, 4
* ```scala sc:compile
* val a = List(1,2,3,4) // List(1, 2, 3, 4)
* val b = new StringBuilder()
* a.addString(b, ", ") // 1, 2, 3, 4
* ```
*
* @param b the string builder to which elements are appended.
Expand All @@ -1433,15 +1417,10 @@ transparent trait IterableOnceOps[+A, +CC[_], +C] extends Any { this: IterableOn
*
* Example:
*
* ```
* scala> val a = List(1,2,3,4)
* a: List[Int] = List(1, 2, 3, 4)
*
* scala> val b = new StringBuilder()
* b: StringBuilder =
*
* scala> val h = a.addString(b)
* h: StringBuilder = 1234
* ```scala sc:compile
* val a = List(1,2,3,4) // List(1, 2, 3, 4)
* val b = new StringBuilder()
* val h = a.addString(b) // 1234
* ```
*
* @param b the string builder to which elements are appended.
Expand All @@ -1452,7 +1431,13 @@ transparent trait IterableOnceOps[+A, +CC[_], +C] extends Any { this: IterableOn
/** Given a collection factory `factory`, converts this $coll to the appropriate
* representation for the current element type `A`. Example uses:
*
* ```scala sc-name:import-and-xs sc-hidden
* import scala.collection.mutable.ArrayBuffer
* import scala.collection.immutable.BitSet
* val xs: Iterable[Int] = Seq(1, 2, 3, 4, 5)
* ```
*
* ```scala sc-compile-with:import-and-xs
* xs.to(List)
* xs.to(ArrayBuffer)
* xs.to(BitSet) // for xs: Iterable[Int]
Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/collection/Iterator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import caps.unsafe.untrackedCaptures
*
* Consider this example for safe and unsafe use:
*
* ```
* ```scala sc:compile
* def f[A](it: Iterator[A]) = {
* if (it.hasNext) { // Safe to reuse "it" after "hasNext"
* it.next() // Safe to reuse "it" after "next"
Expand Down
25 changes: 12 additions & 13 deletions library/src/scala/collection/JavaConverters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,26 @@ import scala.language.implicitConversions
* ```
* In all cases, converting from a source type to a target type and back
* again will return the original source object. For example:
* ```
```scala sc-hidden sc-name:import-java-converters
* import scala.collection.JavaConverters._
* ```
* ```scala sc-compile-with:import-java-converters
*
* val source = new scala.collection.mutable.ListBuffer[Int]
* val target: java.util.List[Int] = source.asJava
* val other: scala.collection.mutable.Buffer[Int] = target.asScala
* assert(source eq other)
* ```
* Alternatively, the conversion methods have descriptive names and can be invoked explicitly.
* ```
* scala> val vs = java.util.Arrays.asList("hi", "bye")
* vs: java.util.List[String] = [hi, bye]
*
* scala> val ss = asScalaIterator(vs.iterator)
* ss: Iterator[String] = <iterator>
*
* scala> .toList
* res0: List[String] = List(hi, bye)
*
* scala> val ss = asScalaBuffer(vs)
* ss: scala.collection.mutable.Buffer[String] = Buffer(hi, bye)
* ```scala sc-compile-with:import-java-converters
* val vs = java.util.Arrays.asList("hi", "bye")
* // vs: java.util.List[String] = [hi, bye]
* val ss = asScalaIterator(vs.iterator)
* // ss: Iterator[String] = <iterator>
* ss.toList
* // res0: List[String] = List(hi, bye)
* val ss2 = asScalaBuffer(vs)
* // ss2: scala.collection.mutable.Buffer[String] = Buffer(hi, bye)
* ```
*/
@deprecated("Use `scala.jdk.CollectionConverters` instead", "2.13.0")
Expand Down
15 changes: 5 additions & 10 deletions library/src/scala/collection/Map.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,12 @@ trait Map[K, +V]
* to unexpected results if `ordering.equiv(k1, k2)` (used for lookup in `TreeMap`) is different from `k1 == k2`
* (used for lookup in `HashMap`).
*
* ```scala sc:compile
* import scala.collection.immutable._
* val ord: Ordering[String] = _ compareToIgnoreCase _
* val result1 = TreeMap("A" -> 1)(using ord) == HashMap("a" -> 1) // false
* val result2 = HashMap("a" -> 1) == TreeMap("A" -> 1)(using ord) // true
* ```
* scala> import scala.collection.immutable._
* scala> val ord: Ordering[String] = _ compareToIgnoreCase _
*
* scala> TreeMap("A" -> 1)(ord) == HashMap("a" -> 1)
* val res0: Boolean = false
*
* scala> HashMap("a" -> 1) == TreeMap("A" -> 1)(ord)
* val res1: Boolean = true
* ```
*
*
* @param o The map to which this map is compared
* @return `true` if the two maps are equal according to the description
Expand Down
4 changes: 1 addition & 3 deletions library/src/scala/collection/Searching.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ object Searching {
*
* Example usage:
*
* ```
* ```scala sc:compile
* val list = List(1, 3, 4, 5) // list must be sorted before searching
* list.search(4) // Found(2)
* list.search(2) // InsertionPoint(1)
* ```
*
*
*/
sealed abstract class SearchResult {
/** The index corresponding to the element searched for in the sequence, if it was found,
Expand Down
47 changes: 18 additions & 29 deletions library/src/scala/collection/Seq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ transparent trait SeqOps[+A, +CC[_], +C] extends Any
override def view: SeqView[A]^{this} = new SeqView.Id[A](this)

/** Gets the element at the specified index. This operation is provided for convenience in `Seq`. It should
* not be assumed to be efficient unless you have an `IndexedSeq`.
* not be assumed to be efficient unless you have an `IndexedSeq`.
*/
@throws[IndexOutOfBoundsException]
def apply(i: Int): A
Expand All @@ -96,15 +96,10 @@ transparent trait SeqOps[+A, +CC[_], +C] extends Any
* Also, the original $coll is not modified, so you will want to capture the result.
*
* Example:
* ```
* scala> val x = List(1)
* x: List[Int] = List(1)
*
* scala> val y = 2 +: x
* y: List[Int] = List(2, 1)
*
* scala> println(x)
* List(1)
* ```scala sc:compile
* val x = List(1)
* val y = 2 +: x
* // x is still List(1), y is List(2, 1)
* ```
*
* @tparam B the element type of the returned $coll.
Expand All @@ -127,17 +122,11 @@ transparent trait SeqOps[+A, +CC[_], +C] extends Any
* $willNotTerminateInf
*
* Example:
* ```
* scala> val a = List(1)
* a: List[Int] = List(1)
*
* scala> val b = a :+ 2
* b: List[Int] = List(1, 2)
*
* scala> println(a)
* List(1)
* ```
*
* ```scala sc:compile
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this an improvement? perhaps the sc:compile mode should support REPL session syntax

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, that would be nice, but I think we should merge this anyway, rather than wait for a speculative future improvement. I rather liked the REPL version, but this way seems approximately as good to me, and it gets us the checking now.

* val a = List(1)
* val b = a :+ 2
* // a is still List(1), b is List(1, 2)
* ``` *
* @tparam B the element type of the returned $coll.
* @param elem the appended element
* @return a new $coll consisting of
Expand Down Expand Up @@ -537,7 +526,7 @@ transparent trait SeqOps[+A, +CC[_], +C] extends Any
* $willForceEvaluation
*
* @return An Iterator which traverses the distinct permutations of this $coll.
* @example ```
* @example ```scala sc:compile
* Seq('a', 'b', 'b').permutations.foreach(println)
* // List(a, b, b)
* // List(b, a, b)
Expand Down Expand Up @@ -574,7 +563,7 @@ transparent trait SeqOps[+A, +CC[_], +C] extends Any
* $willForceEvaluation
*
* @return An Iterator which traverses the n-element combinations of this $coll.
* @example ```
* @example ```scala sc:compile
* Seq('a', 'b', 'b', 'b', 'c').combinations(2).foreach(println)
* // List(a, b)
* // List(a, c)
Expand Down Expand Up @@ -752,9 +741,9 @@ transparent trait SeqOps[+A, +CC[_], +C] extends Any
* the desired ordering.
* @return a $coll consisting of the elements of this $coll
* sorted according to the comparison function `lt`.
* @example ```
* List("Steve", "Bobby", "Tom", "John", "Bob").sortWith((x, y) => x.take(3).compareTo(y.take(3)) < 0) =
* List("Bobby", "Bob", "John", "Steve", "Tom")
* @example ```scala sc:compile
* List("Steve", "Bobby", "Tom", "John", "Bob").sortWith((x, y) => x.take(3).compareTo(y.take(3)) < 0)
* // List("Bobby", "Bob", "John", "Steve", "Tom")
* ```
*/
def sortWith(lt: (A, A) => Boolean): C^{this} = sorted(using Ordering.fromLessThan(lt))
Expand All @@ -777,11 +766,11 @@ transparent trait SeqOps[+A, +CC[_], +C] extends Any
* sorted according to the ordering where `x < y` if
* `ord.lt(f(x), f(y))`.
*
* @example ```
* @example ```scala sc:compile
* val words = "The quick brown fox jumped over the lazy dog".split(' ')
* // this works because scala.Ordering will implicitly provide an Ordering[Tuple2[Int, Char]]
* words.sortBy(x => (x.length, x.head))
* res0: Array[String] = Array(The, dog, fox, the, lazy, over, brown, quick, jumped)
* val sorted = words.sortBy(x => (x.length, x.head))
* // sorted: Array[String] = Array(The, dog, fox, the, lazy, over, brown, quick, jumped)
* ```
*/
def sortBy[B](f: A => B)(implicit ord: Ordering[B]): C^{this} = sorted(using ord.on(f))
Expand Down
15 changes: 5 additions & 10 deletions library/src/scala/collection/Set.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,12 @@ trait Set[A]
* to unexpected results if `ordering.equiv(e1, e2)` (used for lookup in `TreeSet`) is different from `e1 == e2`
* (used for lookup in `HashSet`).
*
* ```scala sc:compile
* import scala.collection.immutable._
* val ord: Ordering[String] = _ compareToIgnoreCase _
* val result1 = TreeSet("A")(using ord) == HashSet("a") // false
* val result2 = HashSet("a") == TreeSet("A")(using ord) // true
* ```
* scala> import scala.collection.immutable._
* scala> val ord: Ordering[String] = _ compareToIgnoreCase _
*
* scala> TreeSet("A")(ord) == HashSet("a")
* val res0: Boolean = false
*
* scala> HashSet("a") == TreeSet("A")(ord)
* val res1: Boolean = true
* ```
*
*
* @param that The set to which this set is compared
* @return `true` if the two sets are equal according to the description
Expand Down
Loading
Loading