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
42 changes: 18 additions & 24 deletions library/src/scala/jdk/Accumulator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,15 @@ import scala.language.implicitConversions
* Note: to run the example, start the Scala REPL with `scala -Yrepl-class-based` to avoid
* deadlocks, see [[https://github.com/scala/bug/issues/9076]].
*
* ```
* scala> import scala.jdk.StreamConverters._
* import scala.jdk.StreamConverters._
*
* scala> def isPrime(n: Int): Boolean = !(2 +: (3 to Math.sqrt(n).toInt by 2) exists (n % _ == 0))
* isPrime: (n: Int)Boolean
* ```scala sc:compile
* import scala.jdk.StreamConverters.*
*
* scala> val intAcc = (1 to 10000).asJavaParStream.filter(isPrime).toScala(scala.jdk.Accumulator)
* intAcc: scala.jdk.IntAccumulator = IntAccumulator(1, 3, 5, 7, 11, 13, 17, 19, ...
* def isPrime(n: Int): Boolean = n > 1 && !(2 +: (3 to Math.sqrt(n).toInt by 2)).exists(n % _ == 0)
*
* scala> val stringAcc = (1 to 100).asJavaParStream.mapToObj("<>" * _).toScala(Accumulator)
* stringAcc: scala.jdk.AnyAccumulator[String] = AnyAccumulator(<>, <><>, <><><>, ...
* val intAcc = (1 to 10000).asJavaParStream.filter(isPrime).toScala(scala.jdk.Accumulator)
* // intAcc: scala.jdk.IntAccumulator = IntAccumulator(1, 3, 5, 7, 11, 13, 17, 19, ...
* val stringAcc = (1 to 100).asJavaParStream.mapToObj("<>" * _).toScala(Accumulator)
* // stringAcc: scala.jdk.AnyAccumulator[String] = AnyAccumulator(<>, <><>, <><><>, ...
* ```
*
* There are two possibilities to process elements of a primitive Accumulator without boxing:
Expand Down Expand Up @@ -151,18 +148,15 @@ abstract class Accumulator[@specialized(Double, Int, Long) A, +CC[X] <: mutable.
* the implicit [[Accumulator.AccumulatorFactoryShape]] instance is used to build a specialized
* Accumulator according to the element type:
*
* ```
* scala> val intAcc = Accumulator(1,2,3)
* intAcc: scala.collection.convert.IntAccumulator = IntAccumulator(1, 2, 3)
*
* scala> val anyAccc = Accumulator("K")
* anyAccc: scala.collection.convert.AnyAccumulator[String] = AnyAccumulator(K)
*
* scala> val intAcc2 = List(1,2,3).to(Accumulator)
* intAcc2: scala.jdk.IntAccumulator = IntAccumulator(1, 2, 3)
*
* scala> val anyAcc2 = List("K").to(Accumulator)
* anyAcc2: scala.jdk.AnyAccumulator[String] = AnyAccumulator(K)
* ```scala sc:compile
* val intAcc = Accumulator(1, 2, 3)
* // intAcc: scala.collection.convert.IntAccumulator = IntAccumulator(1, 2, 3)
* val anyAcc = Accumulator("K")
* // anyAccc: scala.collection.convert.AnyAccumulator[String] = AnyAccumulator(K)
* val intAcc2 = List(1, 2, 3).to(Accumulator)
* // intAcc2: scala.jdk.IntAccumulator = IntAccumulator(1, 2, 3)
* val anyAcc2 = List("K").to(Accumulator)
* // anyAcc2: scala.jdk.AnyAccumulator[String] = AnyAccumulator(K)
* ```
*
* @define coll Accumulator
Expand Down Expand Up @@ -260,7 +254,7 @@ object Accumulator {
* @param elem the element computation
* @return An $coll that contains the results of `n1 x n2` evaluations of `elem`.
*/
def fill[A, C](n1: Int, n2: Int)(elem: => A)(implicit canAccumulate: AccumulatorFactoryShape[A, C]): AnyAccumulator[C] =
def fill[A, C](n1: Int, n2: Int)(elem: => A)(implicit canAccumulate: AccumulatorFactoryShape[A, C]): AnyAccumulator[C] =
fill(n1)(fill(n2)(elem)(using canAccumulate))(using AccumulatorFactoryShape.anyAccumulatorFactoryShape[C])

/** Produces a three-dimensional $coll containing the results of some element computation a number of times.
Expand Down Expand Up @@ -355,7 +349,7 @@ object Accumulator {
* @param xss the collections that are to be concatenated.
* @return the concatenation of all the collections.
*/
def concat[A, C](xss: Iterable[A]*)(implicit canAccumulate: AccumulatorFactoryShape[A, C]): C =
def concat[A, C](xss: Iterable[A]*)(implicit canAccumulate: AccumulatorFactoryShape[A, C]): C =
if (xss.isEmpty) canAccumulate.empty
else {
val b = canAccumulate.factory.newBuilder
Expand Down
37 changes: 15 additions & 22 deletions library/src/scala/jdk/CollectionConverters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,20 @@ import scala.collection.convert.{AsJavaExtensions, AsScalaExtensions}
* Note: to create [[java.util.stream.Stream Java Streams]] that operate on Scala collections
* (sequentially or in parallel), use [[StreamConverters]].
*
* ```
* import scala.jdk.CollectionConverters._
* val s: java.util.Set[String] = Set("one", "two").asJava
* ```scala sc:compile
* import scala.jdk.CollectionConverters.*
* val s: java.util.Set[String] = Set("one", "two").asJava
* ```
*
* The conversions return adapters for the corresponding API, i.e., the collections are wrapped,
* not copied. Changes to the original collection are reflected in the view, and vice versa:
*
* ```
* scala> import scala.jdk.CollectionConverters._
*
* scala> val s = collection.mutable.Set("one")
* s: scala.collection.mutable.Set[String] = HashSet(one)
*
* scala> val js = s.asJava
* js: java.util.Set[String] = [one]
*
* scala> js.add("two")
*
* scala> s
* res2: scala.collection.mutable.Set[String] = HashSet(two, one)
* ```scala sc:compile
* import scala.jdk.CollectionConverters.*
* val s = collection.mutable.Set("one")
* val js = s.asJava
* js.add("two")
* assert(s == collection.mutable.Set("one", "two"))
* ```
*
* The following conversions are supported via `asScala` and `asJava`:
Expand Down Expand Up @@ -84,13 +77,13 @@ import scala.collection.convert.{AsJavaExtensions, AsScalaExtensions}
* In all cases, converting from a source type to a target type and back
* again will return the original source object. For example:
*
* ```
* import scala.jdk.CollectionConverters._
* ```scala sc:compile
* import scala.jdk.CollectionConverters.*
*
* 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)
* 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)
* ```
*/
object CollectionConverters extends AsJavaExtensions with AsScalaExtensions
30 changes: 15 additions & 15 deletions library/src/scala/jdk/FunctionConverters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,30 @@ import scala.language.`2.13`
* Using the `.asJava` extension method on a Scala function produces the most specific possible
* Java function type:
*
* ```
* scala> import scala.jdk.FunctionConverters._
* scala> val f = (x: Int) => x + 1
*
* scala> val jf1 = f.asJava
* jf1: java.util.function.IntUnaryOperator = ...
* ```scala sc:compile
* import scala.jdk.FunctionConverters.*
* val f = (x: Int) => x + 1
* val jf1 = f.asJava
* ```
*
* More generic Java function types can be created using the corresponding `asJavaXYZ` extension
* method:
*
* ```
* scala> val jf2 = f.asJavaFunction
* jf2: java.util.function.Function[Int,Int] = ...
*
* scala> val jf3 = f.asJavaUnaryOperator
* jf3: java.util.function.UnaryOperator[Int] = ...
* ```scala sc:compile
* import scala.jdk.FunctionConverters.*
* val f = (x: Int) => x + 1
* val jf2 = f.asJavaFunction
* val jf3 = f.asJavaUnaryOperator
* ```
*
* Converting a Java function to Scala is done using the `asScala` extension method:
*
* ```
* scala> List(1,2,3).map(jf2.asScala)
* res1: List[Int] = List(2, 3, 4)
* ```scala sc:compile
* import scala.jdk.FunctionConverters.*
* val f = (x: Int) => x + 1
* val jf2 = f.asJavaFunction
* val mapped = List(1, 2, 3).map(jf2.asScala)
* assert(mapped == List(2, 3, 4))
* ```
*/
object FunctionConverters extends Priority0FunctionExtensions
22 changes: 11 additions & 11 deletions library/src/scala/jdk/OptionConverters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ import java.util.{Optional, OptionalDouble, OptionalInt, OptionalLong}
*
* Example usage:
*
* ```
* import scala.jdk.OptionConverters._
* val a = Option("example").toJava // Creates java.util.Optional[String] containing "example"
* val b = (None: Option[String]).toJava // Creates an empty java.util.Optional[String]
* val c = a.toScala // Back to Option("example")
* val d = b.toScala // Back to None typed as Option[String]
* val e = Option(2.7).toJava // java.util.Optional[Double] containing boxed 2.7
* val f = Option(2.7).toJavaPrimitive // java.util.OptionalDouble containing 2.7 (not boxed)
* val g = f.toScala // Back to Option(2.7)
* val h = f.toJavaGeneric // Same as e
* val i = e.toJavaPrimitive // Same as f
* ```scala sc:compile
* import scala.jdk.OptionConverters.*
* val a = Option("example").toJava // Creates java.util.Optional[String] containing "example"
* val b = (None: Option[String]).toJava // Creates an empty java.util.Optional[String]
* val c = a.toScala // Back to Option("example")
* val d = b.toScala // Back to None typed as Option[String]
* val e = Option(2.7).toJava // java.util.Optional[Double] containing boxed 2.7
* val f = Option(2.7).toJavaPrimitive // java.util.OptionalDouble containing 2.7 (not boxed)
* val g = f.toScala // Back to Option(2.7)
* val h = f.toJavaGeneric // Same as e
* val i = e.toJavaPrimitive // Same as f
* ```
*/
object OptionConverters {
Expand Down
24 changes: 11 additions & 13 deletions library/src/scala/jdk/StreamConverters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,23 @@ import scala.collection.convert.StreamExtensions
*
* The methods `asJavaSeqStream` and `asJavaParStream` convert a collection to a Java Stream:
*
* ```
* scala> import scala.jdk.StreamConverters._
*
* scala> val s = (1 to 10).toList.asJavaSeqStream
* s: java.util.stream.IntStream = java.util.stream.IntPipeline\$Head@7b1e5e55
* ```scala sc:compile
* import scala.jdk.StreamConverters.*
*
* scala> s.map(_ * 2).filter(_ > 5).toScala(List)
* res1: List[Int] = List(6, 8, 10, 12, 14, 16, 18, 20)
* val s = (1 to 10).toList.asJavaSeqStream
* // s: java.util.stream.IntStream = java.util.stream.IntPipeline\$Head@7b1e5e55
* val doubled = s.map(_ * 2).filter(_ > 5).toScala(List)
* assert(doubled == List(6, 8, 10, 12, 14, 16, 18, 20))
* ```
*
* Note: using parallel streams in the Scala REPL causes deadlocks, see
* [[https://github.com/scala/bug/issues/9076]]. As a workaround, use `scala -Yrepl-class-based`.
*
* ```
* scala> def isPrime(n: Int): Boolean = !(2 +: (3 to Math.sqrt(n).toInt by 2) exists (n % _ == 0))
* isPrime: (n: Int)Boolean
*
* scala> (10000 to 1000000).asJavaParStream.filter(isPrime).toScala(Vector)
* res6: scala.collection.immutable.Vector[Int] = Vector(10007, 10009, 10037, 10039, ...
* ```scala sc:compile
* import scala.jdk.StreamConverters.*
* def isPrime(n: Int): Boolean = n > 1 && !(2 +: (3 to Math.sqrt(n).toInt by 2)).exists(n % _ == 0)
* val primes = (10000 to 10100).asJavaParStream.filter(isPrime).toScala(Vector)
* // primes: scala.collection.immutable.Vector[Int] = Vector(10007, 10009, 10037, 10039, ...
* ```
*
* A Java [[Stream]] provides operations on a sequence of elements. Streams are created from
Expand Down
Loading