This is part of a 'brown-bag' for my colleagues to extol the joys of functional programming. I want to emphasize practicality over theory.
One use case is to do some IO then close all streams. For this, Applicatives and Cats made the code incredibly easy to work with.
Without further ado, here's the code:
import cats.data.NonEmptyList
import cats.{Applicative, ApplicativeError}
class MyIO[F[_]: Applicative](implicit E: ApplicativeError[F, Throwable]) {
def doIO[U](f: => U): F[U] = E.fromTry(Try(f))
def allOrNothing[A](xs: NonEmptyList[F[A]]): F[A] = {
import cats.implicits._
xs.foldLeft(xs.head) { case (a, x) =>
a*> x
}
}
def doIO(...): F[Unit] = {
...
val fo1 = doIO(firstOutputStream.close())
val fo2 = doIO(secondOutputString.close())
val fi = doIO(inputStream.close())
allOrNothing(NonEmptyList(fo1, List(fo2, fi)))
}
A few points to note here.
- This works for all Applicative types. That is to say, with the relevant imports, doIO will return us an Either, an Option, or any other suitable data structure with no changes to the code in MyIO. (Note: Option may not be an appropriate choice...)
- Because allOrNothing is dealing with Applicatives, it will return the relevant type for all passing (Right, Some, etc) or for at least one failing (Left, None, etc) again with no change to the code.
Note that there must be a suitable ApplicationError[F[_], Exception] in the ether. Some you can get for free with Cats and a suitable import, some you must roll by hand.
So, what is actually returned in allOrNothing? Given a list of, say, Rights, it returns the last Right. But given a List of Eithers with at least one Left, then the first Left will be returned. An analogous situation exists for any type with the appropriate ApplicationError.
No comments:
Post a Comment