Skip to content

Instantly share code, notes, and snippets.

@alexandru
Created September 7, 2016 14:10
Show Gist options
  • Select an option

  • Save alexandru/6bbaf7d1a7edef290e17696713ef9e97 to your computer and use it in GitHub Desktop.

Select an option

Save alexandru/6bbaf7d1a7edef290e17696713ef9e97 to your computer and use it in GitHub Desktop.
sealed trait State { def count: Int }
case class Odd(count: Int) extends State
case class Even(count: Int) extends State
case class Emit(count: Int) extends State
val scanned =
observable.scan(Empty : State) { (state, elem) =>
val newState = state match {
case Odd(count) =>
val s = if (elem % 2 == 1) Odd(count+1) else Even(1)
case Even(count) =>
if (elem % 2 == 0) Even(count+1) else Odd(1)
case Emit(_) =>
if (elem % 2 == 0) Even(1) else Odd(1)
}
if (newState.count > 100)
Emit(newState.count)
else
newState
}
scanned.collect { case Emit(n) => true }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment