Skip to content

Instantly share code, notes, and snippets.

@searler
Created April 25, 2015 14:48
Show Gist options
  • Save searler/01f480faca0ab7fe588b to your computer and use it in GitHub Desktop.
Save searler/01f480faca0ab7fe588b to your computer and use it in GitHub Desktop.
RxJava groupBy and scan

This code is the RxJava equivalent of the Spark updateStateByKey operation

//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