@@ -677,18 +677,27 @@ def feed[T, R](array: Array[T]) { reader: () => R / read[T] }: R = {
677677 }
678678}
679679
680+ /// Collects elements emitted by a push stream producer `stream` into a new array,
681+ /// discarding the result of the producer.
682+ /// The provided capacity is a hint for the expected number of elements.
683+ /// It must be strictly larger than zero.
684+ def collect[A](capacity: Int) { stream: () => Unit / emit[A] }: Array[A] =
685+ returning::collect[A, Unit](capacity){stream}.second
686+
680687/// Collects elements emitted by a push stream producer `stream` into a new array,
681688/// discarding the result of the producer.
682689def collect[A] { stream: () => Unit / emit[A] }: Array[A] =
683- returning:: collect[A, Unit] {stream}.second
690+ collect[A](4) {stream}
684691
685692namespace returning {
686693
687694 /// Collects elements emitted by a push stream producer `stream` into a new array,
688695 /// returning the result of the producer as well as the collected array.
689- def collect[A, R] { stream: () => R / emit[A] }: (R, Array[A]) = {
696+ /// The provided capacity is a hint for the expected number of elements.
697+ /// It must be strictly larger than zero.
698+ def collect[A, R](capacity: Int) { stream: () => R / emit[A] }: (R, Array[A]) = {
690699 var i = 0
691- var a = unsafeAllocate(1 )
700+ var a = unsafeAllocate(capacity )
692701 try {
693702 (stream(), a.resize(i))
694703 } with emit[A] { (v) =>
@@ -698,4 +707,10 @@ namespace returning {
698707 resume(())
699708 }
700709 }
710+
711+ /// Collects elements emitted by a push stream producer `stream` into a new array,
712+ /// returning the result of the producer as well as the collected array.
713+ def collect[A, R] { stream: () => R / emit[A] }: (R, Array[A]) =
714+ returning::collect[A, R](4){stream}
715+
701716}
0 commit comments