Part 1
Cats Core, Part 1: From Optional Fields to Useful Validation Errors
I started learning Cats Core with a small policy form. The same mapN operation first combines optional fields, then collects useful validation errors with Validated.
Series
Learning Typelevel Cats Core in Scala 2 through runnable examples: combining optional values, collecting validation errors, and understanding the type classes behind practical operations.
Part 1
I started learning Cats Core with a small policy form. The same mapN operation first combines optional fields, then collects useful validation errors with Validated.
Part 2
I could see Validated collecting errors, but I wanted to see the method call behind it. I made its Semigroup argument explicit, then supplied a combination rule for my own structured errors.
Part 3
I knew how to combine two error reports. Monoid gives an empty batch a useful result, and foldMap takes the same idea to a shopping cart: turn each line into a contribution, then combine the totals.
Part 4
List and Option already know how to map. I give my own Quote a Functor instance, reuse the same discount function, and trace why identity and composition matter.
Part 5
A price and a delivery cost are both optional, and nested map gives me Some(Some(15)). Apply combines independent values into one result, and Applicative adds the starting value a whole cart needs.
Part 6
What the Nec in ValidatedNec guarantees, why errors.toList failed to compile in my Play controller, and when NonEmptyList is the better container.
Part 7
In Part 4 I wrote map[A, B](fa: Quote[A])(f: A => B) and moved on, but the letters kept slowing me down. A short break from new type classes: two rules that decode most Cats signatures, and the one name that breaks them.
Part 8
I wrote a tiny Todo validator with cats' ValidatedNec and it made one rule click: you can only accumulate errors when the steps don't depend on each other. That's the whole difference between an Applicative and a Monad.
Part 9
Scala's == compares any two values, so a TodoId against a String compiles and returns false. Cats' Eq makes that a type error, explains why Some(1) === None fails, and lets me decide which fields count as equal.
Part 10
Cats' Order gives you compare, min, max and sorting, and because it extends Eq, the fields you order by also decide what === treats as equal. I also explain why the standard library's .sorted needs .toOrdering, except when one particular import is in scope.
Part 11
Either holds an error or a value. Ior can hold both, which is what an import needs when a contract is accepted but something about it deserves a warning.