day 5 

Derived from Bello Nock's Sky Walk by Chris Phutully

Yesterday we reviewed Semigroup and Monoid, implementing custom monoids along the way. We also looked at Foldable that can foldMap etc.

Apply.ap 

Starting with a few updates today. First, Apply.apply that we looked at in day 3 has been renamed to Apply.ap #308.

Serializable typeclass instance 

Second, do you remember that checking the monoid laws on a value class kept tripping on Serializable? That turned out not to be Cats’ fault. I went into Cats’ gitter chat and Erik (@d6/@non) kindly pointed out that the reason my typeclass instances are not serializable is because they are defined on the REPL. Once I moved First to src/ the laws passed fine:

scala> import cats._, cats.instances.all._
import cats._
import cats.instances.all._

scala> import algebra.laws.GroupLaws
import algebra.laws.GroupLaws

scala> import org.scalacheck.{ Arbitrary, Gen }
import org.scalacheck.{Arbitrary, Gen}

scala> import example.First
import example.First

scala> implicit def arbFirst[A: Eq](implicit ev: Arbitrary[Option[A]]): Arbitrary[First[A]] =
     |          Arbitrary { ev.arbitrary map { First(_) } }
arbFirst: [A](implicit evidence$1: cats.Eq[A], implicit ev: org.scalacheck.Arbitrary[Option[A]])org.scalacheck.Arbitrary[example.First[A]]

scala> val rs = GroupLaws[First[Int]].monoid
rs: algebra.laws.GroupLaws[example.First[Int]]#GroupProperties = algebra.laws.GroupLaws$GroupProperties@77fac6ab

scala> rs.all.check
+ monoid.associativity: OK, passed 100 tests.
+ monoid.combineAll(Nil) == id: OK, passed 100 tests.
+ monoid.combineN(a, 0) == id: OK, passed 100 tests.
+ monoid.combineN(a, 1) == a: OK, passed 100 tests.
+ monoid.combineN(a, 2) == a |+| a: OK, passed 100 tests.
+ monoid.isEmpty: OK, passed 100 tests.
+ monoid.leftIdentity: OK, passed 100 tests.
+ monoid.rightIdentity: OK, passed 100 tests.
+ monoid.serializable: OK, proved property.

Jason Zaugg (@retronym) also pointed that, to support serialization beyond precisely the same version of Cats on both sides of the wire, we need to do more things like: