Back to Fpinscala

12.Answer

answerkey/streamingio/12.answer.md

latest945 B
Original Source

Let's first consider how attempt interacts with the accessible constructors -- i.e. unit and raiseError:

scala
unit(a).attempt == unit(Right(a))
raiseError(t).attempt == unit(Left(t))

And how handleErrorWith interacts with each constructor:

scala
unit(a).handleErrorWith(h) == unit(a)
raiseError(t).handleErrorWith(h) == h(t)

And how raiseError interacts with flatMap:

scala
raiseError(t).flatMap(f) == raiseError(t)

We could also make some stronger demands on how exceptions are handled. For example, should unit catch exceptions thrown from its thunk argument?

scala
unit(throw new Exception) == raiseError(new Exception)

Should exceptions be caught if thrown from the functions passed to map and flatMap?

scala
fa.map(_ => throw new Exception) == raiseError(new Exception)
fa.flatMap(_ => throw new Exception) == raiseError(new Exception)

There's no wrong answer, only tradeoffs.