Cats is to Scala what Guava is to Java : a must to know library ! But unfortunately, learning cats is not so easy.
This hand's on focuses on concrete use cases instead of complex theory. The goal is to show that you can use cats to improve your code and remove some boilerplate.
I've chosen some pieces of code that can benefit from cats. For each, a test suite will help you understand what the code does.
sbt test
- add the dependency in
build.sbt
libraryDependencies += "org.typelevel" %% "cats-core" % "2.9.0"
- import stuff
Your mission, if you accept it, is to implement all methods marked as todo using Cats. All the unit tests are provided and you don't need to modify them. When your implementation will be correct, the tests will pass.
Here are some hints if you need them.
- replace
Try
withcats.Validation
- Hint : Monoid (don't worry, it's such a pedantic word to say a type that you can merge).
trait Monoid[F] { self =>
def append(f1: F, f2: => F): F
}
- Hint : use
Traverse
to transformF[G[A]]
intoG[F[A]]
- Hint : use
OptionT
, a tool for composing monads.
- Hint : use a Reader monad, to inject the dependency.