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

Commit 96e14bd

Browse files
committed
Pair renamed to Step to avoid clashing with Predef.Pair
1 parent e18fc50 commit 96e14bd

File tree

6 files changed

+29
-29
lines changed

6 files changed

+29
-29
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name := "asyncstreams"
22

3-
version := "0.3"
3+
version := "0.3.5"
44

55
scalaVersion := "2.11.8"
66

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ asyncstreams is available via jitpack:
2323
```
2424
resolvers += "jitpack" at "https://jitpack.io"
2525
26-
libraryDependencies += "com.github.danslapman" %% "asyncstreams" % "0.3"
26+
libraryDependencies += "com.github.danslapman" %% "asyncstreams" % "0.3.5"
2727
```
2828

2929
asyncstreams is based on [scala-async](https://github.com/iboltaev/scala-async) ideas.

src/main/scala/asyncstreams/AsyncStream.scala

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import scala.collection.generic.CanBuildFrom
66
import scala.concurrent.{ExecutionContext, Future}
77
import scala.language.higherKinds
88

9-
case class AsyncStream[A](data: Future[Pair[A, AsyncStream[A]]]) {
9+
case class AsyncStream[A](data: Future[Step[A, AsyncStream[A]]]) {
1010
import AsyncStream._
1111

1212
def foldLeft[B](start: B)(f: (B, A) => B)(implicit executor: ExecutionContext): Future[B] = {
13-
def impl(d: Future[Pair[A, AsyncStream[A]]], acc: Future[B]): Future[B] =
13+
def impl(d: Future[Step[A, AsyncStream[A]]], acc: Future[B]): Future[B] =
1414
d.flatMap {
1515
case END => acc
16-
case pair => impl(pair.second.data, acc map (b => f(b, pair.first)))
16+
case step => impl(step.rest.data, acc map (b => f(b, step.value)))
1717
}
1818

1919
impl(data, Future(start))
@@ -26,16 +26,16 @@ case class AsyncStream[A](data: Future[Pair[A, AsyncStream[A]]]) {
2626
def takeWhile(p: A => Boolean)(implicit executor: ExecutionContext): AsyncStream[A] =
2727
new AsyncStream[A](data map {
2828
case END => END
29-
case pair if !p(pair.first) => END
30-
case pair => Pair(pair.first, pair.second.takeWhile(p))
29+
case step if !p(step.value) => END
30+
case step => Step(step.value, step.rest.takeWhile(p))
3131
})
3232

3333

3434
def take(n: Int)(implicit executor: ExecutionContext): AsyncStream[A] =
3535
if (n <= 0) nil
3636
else AsyncStream(data.map {
3737
case END => END
38-
case p => Pair(p.first, p.second.take(n - 1))
38+
case p => Step(p.value, p.rest.take(n - 1))
3939
})
4040

4141
def foreach[U](f: (A) => U)(implicit executor: ExecutionContext): Future[Unit] =
@@ -45,12 +45,12 @@ case class AsyncStream[A](data: Future[Pair[A, AsyncStream[A]]]) {
4545
foldLeft(Future(()))((fu: Future[Unit], a: A) => fu.flatMap(_ => f(a)).map(_ => ())).flatMap(u => u)
4646

4747
def flatten[B](implicit asIterable: A => GenIterable[B], executor: ExecutionContext): AsyncStream[B] = {
48-
val streamChunk = (p: Pair[A, AsyncStream[A]]) =>
49-
concat(generate(asIterable(p.first))(it => if (it.nonEmpty) Future(it.head, it.tail) else ENDF), p.second.flatten)
48+
val streamChunk = (p: Step[A, AsyncStream[A]]) =>
49+
concat(generate(asIterable(p.value))(it => if (it.nonEmpty) Future(it.head, it.tail) else ENDF), p.rest.flatten)
5050

5151
AsyncStream(data.flatMap {
5252
case END => ENDF
53-
case pair => streamChunk(pair).data
53+
case step => streamChunk(step).data
5454
})
5555
}
5656
}
@@ -59,18 +59,18 @@ case class AsyncStream[A](data: Future[Pair[A, AsyncStream[A]]]) {
5959
object AsyncStream {
6060
def nil[A](implicit executor: ExecutionContext): AsyncStream[A] = AsyncStream(ENDF)
6161
def single[A](item: A)(implicit executor: ExecutionContext): AsyncStream[A] =
62-
AsyncStream(Future(Pair(item, nil[A])))
62+
AsyncStream(Future(Step(item, nil[A])))
6363

6464
def generate[S, A](start: S)(gen: S => Future[(A, S)])(implicit executor: ExecutionContext): AsyncStream[A] =
6565
AsyncStream(gen(start).map {
6666
case END => END
67-
case (el, rest) => Pair(el, generate(rest)(gen))
67+
case (el, rest) => Step(el, generate(rest)(gen))
6868
})
6969

7070
def concat[A](s1: AsyncStream[A], s2: AsyncStream[A])(implicit executor: ExecutionContext): AsyncStream[A] =
7171
new AsyncStream[A](s1.data.flatMap {
7272
case END => s2.data
73-
case p => Future(Pair(p.first, concat(p.second, s2)))
73+
case step => Future(Step(step.value, concat(step.rest, s2)))
7474
})
7575
}
7676

src/main/scala/asyncstreams/AsyncStreamMonad.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class AsyncStreamMonad(implicit executor: ExecutionContext) extends MonadPlus[As
1616
AsyncStream(
1717
ma.data.flatMap {
1818
case END => ENDF
19-
case pair => f(pair.first).data.map { pair2 =>
20-
Pair(pair2.first, concat(pair2.second, bind(pair.second)(f)))
19+
case step => f(step.value).data.map { step2 =>
20+
Step(step2.value, concat(step2.rest, bind(step.rest)(f)))
2121
}
2222
}
2323
)
@@ -32,19 +32,19 @@ trait AsyncStreamMonadFunctions {
3232
})
3333

3434
def isEmpty[A, S](stream: AsyncStream[A])(implicit ex: ExecutionContext): FState[S, Boolean] =
35-
FState(s => stream.data.map(pair => (pair eq END, s)))
35+
FState(s => stream.data.map(step => (step eq END, s)))
3636

3737
def isEmpty[A, S : FStateMonad](f: S => AsyncStream[A])(implicit fsm: FStateMonad[S], ex: ExecutionContext): FState[S, Boolean] =
3838
fsm.fcondS((s: S) => isEmpty(f(s)))
3939

4040
def notEmpty[A, S](stream: AsyncStream[A])(implicit ex: ExecutionContext): FState[S, Boolean] =
41-
FState(s => stream.data map (pair => (!(pair eq END), s)))
41+
FState(s => stream.data map (step => (!(step eq END), s)))
4242

4343
def notEmpty[A, S](f: S => AsyncStream[A])(implicit fsm: FStateMonad[S], ex: ExecutionContext): FState[S, Boolean] =
4444
fsm.fcondS(s => notEmpty(f(s)))
4545

4646
def get[A, S](stream: AsyncStream[A])(implicit ex: ExecutionContext): FState[S, (A, AsyncStream[A])] =
47-
FState(s => stream.data.map(pair => ((pair.first, pair.second), s)))
47+
FState(s => stream.data.map(step => ((step.value, step.rest), s)))
4848

4949
def generateS[S,A](start: S)(gen: FState[S, A])(implicit ex: ExecutionContext) =
5050
AsyncStream.generate(start)(gen.func)

src/main/scala/asyncstreams/Pair.scala

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package asyncstreams
2+
3+
class Step[A, B](fp: A, sp: => B) {
4+
val value = fp
5+
lazy val rest = sp
6+
}
7+
8+
object Step {
9+
def apply[A, B](value: A, rest: => B) = new Step[A, B](value, rest)
10+
}

0 commit comments

Comments
 (0)