Proposal to add collectors to Java Conversions so that a Java 8 stream can be collected to a Scala List.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Proposal to add collectors to Java Conversions so that a Java 8 stream can be collected to a Scala List.

Gareth Siret
Something like:


  def toList[T]() : Collector[T, ListBuffer[T], List[T]] = {

    val supplier = new Supplier[ListBuffer[T]] {
      override def get() : ListBuffer[T] = {
        new ListBuffer[T]
      }
    }

    val biConsumer = new BiConsumer[ListBuffer[T], T] {
      override def accept(buffer: ListBuffer[T], t : T): Unit = {
        buffer.append(t)
      }
    }

    val combiner = new BinaryOperator[ListBuffer[T]] {
      override def apply(t: ListBuffer[T], u: ListBuffer[T]) : ListBuffer[T] = {
        t.appendAll(u)
        t
      }
    }

    val finisher = new java.util.function.Function[ListBuffer[T], List[T]] {
      override def apply(t : ListBuffer[T]) : List[T] = {
        t.toList
      }
    }
    Collector.of(supplier, biConsumer, combiner, finisher)
  }

Does anyone have any thoughts on whether this is a good plan?

--
You received this message because you are subscribed to the Google Groups "scala-debate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Proposal to add collectors to Java Conversions so that a Java 8 stream can be collected to a Scala List.

Rex Kerr-2
There is something somewhat similar planned for a Java 8 compatibility library.  Check that out (when it's ready) and see if you think it works well enough.

  --Rex

On Wed, Feb 3, 2016 at 12:30 AM, Gareth Siret <[hidden email]> wrote:
Something like:


  def toList[T]() : Collector[T, ListBuffer[T], List[T]] = {

    val supplier = new Supplier[ListBuffer[T]] {
      override def get() : ListBuffer[T] = {
        new ListBuffer[T]
      }
    }

    val biConsumer = new BiConsumer[ListBuffer[T], T] {
      override def accept(buffer: ListBuffer[T], t : T): Unit = {
        buffer.append(t)
      }
    }

    val combiner = new BinaryOperator[ListBuffer[T]] {
      override def apply(t: ListBuffer[T], u: ListBuffer[T]) : ListBuffer[T] = {
        t.appendAll(u)
        t
      }
    }

    val finisher = new java.util.function.Function[ListBuffer[T], List[T]] {
      override def apply(t : ListBuffer[T]) : List[T] = {
        t.toList
      }
    }
    Collector.of(supplier, biConsumer, combiner, finisher)
  }

Does anyone have any thoughts on whether this is a good plan?

--
You received this message because you are subscribed to the Google Groups "scala-debate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scala-debate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Proposal to add collectors to Java Conversions so that a Java 8 stream can be collected to a Scala List.

Rex Kerr-2
By "planned" I mean "I already wrote it and submitted a PR, but it isn't reviewed and polished yet".

  --Rex

On Wed, Feb 3, 2016 at 11:00 AM, Rex Kerr <[hidden email]> wrote:
There is something somewhat similar planned for a Java 8 compatibility library.  Check that out (when it's ready) and see if you think it works well enough.

  --Rex

On Wed, Feb 3, 2016 at 12:30 AM, Gareth Siret <[hidden email]> wrote:
Something like:


  def toList[T]() : Collector[T, ListBuffer[T], List[T]] = {

    val supplier = new Supplier[ListBuffer[T]] {
      override def get() : ListBuffer[T] = {
        new ListBuffer[T]
      }
    }

    val biConsumer = new BiConsumer[ListBuffer[T], T] {
      override def accept(buffer: ListBuffer[T], t : T): Unit = {
        buffer.append(t)
      }
    }

    val combiner = new BinaryOperator[ListBuffer[T]] {
      override def apply(t: ListBuffer[T], u: ListBuffer[T]) : ListBuffer[T] = {
        t.appendAll(u)
        t
      }
    }

    val finisher = new java.util.function.Function[ListBuffer[T], List[T]] {
      override def apply(t : ListBuffer[T]) : List[T] = {
        t.toList
      }
    }
    Collector.of(supplier, biConsumer, combiner, finisher)
  }

Does anyone have any thoughts on whether this is a good plan?

--
You received this message because you are subscribed to the Google Groups "scala-debate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups "scala-debate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.