This code is the RxJava equivalent of the Spark updateStateByKey
operation
Created
April 25, 2015 14:48
-
-
Save searler/01f480faca0ab7fe588b to your computer and use it in GitHub Desktop.
RxJava groupBy and scan
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Accumulate Strings with integer key | |
case class Sum(key: Int, sum: String ="") { | |
def update(value: Char) = this.copy(sum = sum + value) | |
} | |
import rx.lang.scala._ | |
//Source of Char | |
val s:Observable[Char] = _ | |
// Accumulate characters into one of 4 buckets | |
s.groupBy { x => x % 4 } | |
.map { p => p._2.scan(Sum(p._1)) { (s: Sum, v: Char) => s.update(v) } } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment