By using Observer, I'm trying to build the code, which:
1.Generate some (random) values;
2.Combine this values;
3.If someone combined value exceed the threshold, the value have to be passed to another handler. So I expect the resulted value to be returned for further usage
My code:
//generate
val o: Observable[Int] = Observable.repeatEval(Random.nextInt(10))
//handle
val f = o.foldLeft(0) { (acc, el) =>
if (acc < 15) {
el + acc
} else {
println("handled " + acc)
acc
}
}
//use handled
.flatMap{res =>
println("mapped " + res + 1)
Observable(res + 1)
}
But nothing passed to the flatMap-method. The output is following for example:
0
3
7
11
12
handled 20
What am I doing wrong?
You want to use
mapAccumulate
+collect
instead.Use it like:
As I always say, the Scaladoc is your friend.