Skip to content
This repository was archived by the owner on Jun 10, 2021. It is now read-only.

Commit 72d7e35

Browse files
committed
Replace toList with to[Col]
1 parent c9efda1 commit 72d7e35

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/main/scala/asyncstreams/AsyncStream.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package asyncstreams
22

3+
import scala.collection.generic.CanBuildFrom
4+
import scala.annotation.unchecked.{ uncheckedVariance => uV }
35
import scala.concurrent.{ExecutionContext, Future}
6+
import scala.language.higherKinds
47
import scalaz.std.scalaFuture._
58
import scalaz.syntax.std.option._
69
import scalaz.syntax.monad._
@@ -16,8 +19,8 @@ case class AsyncStream[A](data: Future[Chunk[A, AsyncStream[A]]]) {
1619
impl(data, Future(start))
1720
}
1821

19-
def toList(implicit executor: ExecutionContext): Future[List[A]] =
20-
foldLeft[List[A]](Nil)((list, el) => el :: list) map (_.reverse)
22+
def to[Col[_]](implicit executor: ExecutionContext, cbf: CanBuildFrom[Nothing, A, Col[A @uV]]): Future[Col[A]] =
23+
foldLeft(cbf())((col, el) => col += el).map(_.result())
2124

2225
def takeWhile(p: A => Boolean)(implicit executor: ExecutionContext): AsyncStream[A] =
2326
new AsyncStream[A](data map {

src/test/scala/asyncstreams/AsyncStreamTests.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class AsyncStreamTests extends BaseSuite {
2828
val s1 = makeStream(0 :: 1 :: Nil)
2929
val s2 = makeStream(2 :: 3 :: Nil)
3030
val f = concat(s1, s2)
31-
wait(f.toList) shouldBe List(0, 1, 2, 3)
31+
wait(f.to[List]) shouldBe List(0, 1, 2, 3)
3232
}
3333

3434
test("working as monad") {
@@ -40,21 +40,21 @@ class AsyncStreamTests extends BaseSuite {
4040
v2 <- s2
4141
} yield v1 * v2
4242

43-
wait(res.toList) shouldBe List(0, 0, 2, 3)
43+
wait(res.to[List]) shouldBe List(0, 0, 2, 3)
4444
}
4545

4646
test("takeWhile") {
4747
val r = makeInfStream.takeWhile(_ < 4)
48-
wait(r.toList) shouldBe List(0, 1, 2, 3)
48+
wait(r.to[List]) shouldBe List(0, 1, 2, 3)
4949
}
5050

5151
test("take") {
5252
val r = makeInfStream.take(3)
53-
wait(r.toList) shouldBe List(0, 1, 2)
53+
wait(r.to[List]) shouldBe List(0, 1, 2)
5454
}
5555

5656
test("folding large stream should not crash") {
5757
val r = makeInfStream.takeWhile(_ < 1000000)
58-
wait(r.toList) shouldBe (0 to 999999)
58+
wait(r.to[List]) shouldBe (0 to 999999)
5959
}
6060
}

0 commit comments

Comments
 (0)