Invariant Functors Unlocked

We tend to forget this quite often. An invariant functor or an exponential functor is, given A => B and B => A , it converts type A to typeB in the same context F[_]. We call this xmap .

Covariant Functor

Contravariant Functor

Example for Contravariant Functor

Usage

Example for covariant Functor

Note

If type parameters are at contravariant position, that means the method parameters contain the type.

When is invariant functor?

Let’s bring in EncodeJson and DecodeJson into one type class.

EncodeJson and DecodeJson

Functor but invariant

Apply, Applicative to Divide, Divisible

Originally published at gist.github.com.

A software engineer and a functional programming enthusiast at Simple-machines, Sydney, and a hardcore hiking fan. https://twitter.com/afsalt2

A software engineer and a functional programming enthusiast at Simple-machines, Sydney, and a hardcore hiking fan. https://twitter.com/afsalt2