Skip to content

Commit

Permalink
Merge pull request #1239 from zsxwing/apply-scaladoc-example
Browse files Browse the repository at this point in the history
Update docs for "apply" and add an example
  • Loading branch information
benjchristensen committed May 23, 2014
2 parents 0efda07 + ba6bf7c commit d9df3bd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
6 changes: 3 additions & 3 deletions language-adaptors/rxjava-scala/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ This adaptor allows to use RxJava in Scala with anonymous functions, e.g.
```scala
val o = Observable.interval(200 millis).take(5)
o.subscribe(n => println("n = " + n))
Observable(1, 2, 3, 4).reduce(_ + _)
Observable.items(1, 2, 3, 4).reduce(_ + _)
```

For-comprehensions are also supported:

```scala
val first = Observable(10, 11, 12)
val second = Observable(10, 11, 12)
val first = Observable.items(10, 11, 12)
val second = Observable.items(10, 11, 12)
val booleans = for ((n1, n2) <- (first zip second)) yield (n1 == n2)
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,34 @@ class RxScalaDemo extends JUnitSuite {
o.take(1).subscribe(println(_))
}

@Test def createExampleGood2() {
import scala.io.{Codec, Source}

val rxscala = Observable[String](subscriber => {
try {
val input = new java.net.URL("http://rxscala.github.io/").openStream()
subscriber.add(Subscription {
input.close()
})
Source.fromInputStream(input)(Codec.UTF8).getLines()
.takeWhile(_ => !subscriber.isUnsubscribed)
.foreach(subscriber.onNext(_))
if (!subscriber.isUnsubscribed) {
subscriber.onCompleted()
}
}
catch {
case e: Throwable => if (!subscriber.isUnsubscribed) subscriber.onError(e)
}
}).subscribeOn(IOScheduler())

val count = rxscala.flatMap(_.split("\\W+").toSeq.toObservable)
.map(_.toLowerCase)
.filter(_ == "rxscala")
.size
println(s"RxScala appears ${count.toBlockingObservable.single} times in http://rxscala.github.io/")
}

def output(s: String): Unit = println(s)

/** Subscribes to obs and waits until obs has completed. Note that if you subscribe to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3330,10 +3330,19 @@ object Observable {
* Write the function you pass so that it behaves as an Observable: It should invoke the
* Subscriber's `onNext`, `onError`, and `onCompleted` methods appropriately.
*
* You can `add` custom [[Subscription]]s to [[Subscriber]]. These [[Subscription]]s will be called
* <ul>
* <li>when someone calls `unsubscribe`.</li>
* <li>after `onCompleted` or `onError`.</li>
* </ul>
*
* See <a href="http://go.microsoft.com/fwlink/?LinkID=205219">Rx Design Guidelines (PDF)</a> for detailed
* information.
*
* @tparam T
* See `<a href="https://github.com/Netflix/RxJava/blob/master/language-adaptors/rxjava-scala/src/examples/scala/rx/lang/scala/examples/RxScalaDemo.scala">RxScalaDemo</a>.createExampleGood`
* and `<a href="https://github.com/Netflix/RxJava/blob/master/language-adaptors/rxjava-scala/src/examples/scala/rx/lang/scala/examples/RxScalaDemo.scala">RxScalaDemo</a>.createExampleGood2`.
*
* @param T
* the type of the items that this Observable emits
* @param f
* a function that accepts a `Subscriber[T]`, and invokes its `onNext`,
Expand Down

0 comments on commit d9df3bd

Please sign in to comment.