trying to understand some scala syntax, and where to find their spec. Bellow i am confused about statefulMapConcat.
The signature is this one:
def statefulMapConcat[T](f: () => Out => immutable.Iterable[T]): Repr[T]
and we have
"be able to restart" in {
Source(List(2, 1, 3, 4, 1))
.statefulMapConcat(() => {
var prev: Option[Int] = None
x => {
if (x % 3 == 0) throw ex
prev match {
case Some(e) =>
prev = Some(x)
(1 to e).map(_ => x)
case None =>
prev = Some(x)
List.empty[Int]
}
}
})
.withAttributes(ActorAttributes.supervisionStrategy(Supervision.restartingDecider))
.runWith(TestSink.probe[Int])
.request(2)
.expectNext(1, 1)
.request(4)
.expectNext(1, 1, 1, 1)
.expectComplete()
}
I do not under well how
{
var prev: Option[Int] = None
x => {
if (x % 3 == 0) throw ex
prev match {
case Some(e) =>
prev = Some(x)
(1 to e).map(_ => x)
case None =>
prev = Some(x)
List.empty[Int]
}
}
correspond to
Out => immutable.Iterable[T]
Can someone decompose it for me please ?
in my mind x
would correspond to Out
, but then we have the declaration of a variable before var prev: Option[Int] = None
I would like to understand and where to find explanation about those scala magic in general