Created
December 3, 2014 20:52
-
-
Save flatmap13/8d6d1ab24f793d1cac7a to your computer and use it in GitHub Desktop.
reactive look-and-say sequence w/ RxScala
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
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)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://en.wikipedia.org/wiki/Look-and-say_sequence