DEV Community

Viktor Pelle
Viktor Pelle

Posted on

2 2

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)){}
}
Enter fullscreen mode Exit fullscreen mode

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();
})
Enter fullscreen mode Exit fullscreen mode

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. 
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)))
  }
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay