DEV Community is a community of 620,905 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Playing with contravariant functors in typescript

Lets define a sum type `Ordering` with its data constructors:

``````class LT {}
class EQ {}
class GT {}
type Ordering = LT | EQ | GT;
class Comparison<A> {
constructor(public runComparison: ((a: A) => (b: A) => Ordering)){}
}
``````

Because of the position of the parameter we can see that this is a contravariant structure

Example usage:

• We can compare numbers
``````const x = new Comparison<number>((a) => b => {
if (a === b) return new EQ();
if (a > b) return new GT();
if (a < b) return new LT();
})
``````

Now lets define our contravariant functor over Comparison.

As you know functors map has a type of:

``````<A, B>(f: (a: A) => B) => (a: F A) => F B.
``````

And while functors pack things (think of array, or function composition) the contravariant functors "unpack".
Its type is:

``````<A, B>(f: (b: B) => A) => (a: F A) => F B
``````

For `Comparison` this is:

``````  type contramapT = <A, B>(f: (b: B) => A) => (a: Comparison<A>) => Comparison<B>;
const contramap: contramapT = (f) => compC => {
return new Comparison(a => b => compC.runComparison(f(a))(f(b)))
}
``````

Let say we have some users which age we would like to compare.

``````  const compareNumbers = new Comparison<number>((a) => b => {
if (a === b) return new EQ();
if (a > b) return new GT();
if (a < b) return new LT();
});

type userT = {
name: string;
age: number;
}

const user1 = { name: "Igor", age: 25 }
const user2 = { name: "Ivan", age: 28 }

const userAgeComparer = contramap<number, userT>(a => {
return a.age;
})(compareNumbers)
const result = userAgeComparer.runComparison(user1)(user2);
``````

As you can see from this trivial example we generated a new comparer for our user.