Skip to content

Instantly share code, notes, and snippets.

@flatmap13
Created December 3, 2014 20:52
Show Gist options
  • Save flatmap13/8d6d1ab24f793d1cac7a to your computer and use it in GitHub Desktop.
Save flatmap13/8d6d1ab24f793d1cac7a to your computer and use it in GitHub Desktop.
reactive look-and-say sequence w/ RxScala
import rx.lang.scala.Observable
import rx.lang.scala.subjects.BehaviorSubject
object LookAndSay {
def main(args: Array[String]) {
val pipe = BehaviorSubject(Observable.just('1'))
pipe.map(next).foreach(x => {
x.doOnCompleted(println()).foreach(print(_))
pipe.onNext(x)
})
readLine()
}
def next(cs: Observable[Char]): Observable[Char] =
cs.scan ((0, ' ')) ((s, c) => if (s._2 != c) (1, c) else (s._1+1, c))
.drop(1)
.slidingBuffer(2,1)
.filter(x => x.size == 1 || x(0)._2 != x(1)._2)
.flatMap(x => Observable.from(x(0)._1.toString :+ x(0)._2))
}
@flatmap13
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment