0Pricing
TypeScript Academy · Lesson

Generic Functors and Mappers

Write map functions generic over any container.

Generic Functors and Mappers is a free TypeScript Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

A Generic Functor

Now we use the encoding for its purpose: a generic map that works over any registered container. The Functor interface, parameterized by a URI, declares one method.

interface Functor<F extends URIS> {
  readonly URI: F;
  map<A, B>(fa: Kind<F, A>, f: (a: A) => B): Kind<F, B>;
}

The Array Instance

An instance implements map for one URI. The Array instance maps with the built-in array method. Its Kind<"Array", A> resolves to A[].

const arrayFunctor: Functor<"Array"> = {
  URI: "Array",
  map: (fa, f) => fa.map(f)
};

Using the Array Instance

Calling map on the Array instance transforms each element while preserving the array container type.

const doubled = arrayFunctor.map([1, 2, 3], n => n * 2);
// doubled: number[] -> [2, 4, 6]

The Option Type

Define an Option as a value or null and register it. Its map applies the function only when a value is present, otherwise propagates null.

interface URItoKind<A> { Option: A | null }
type Option<A> = A | null;

The Option Instance

The Option instance preserves the option container: a function is applied through the value, and a null stays null.

const optionFunctor: Functor<"Option"> = {
  URI: "Option",
  map: (fa, f) => (fa === null ? null : f(fa))
};

Using the Option Instance

The same map shape now works on an entirely different container. Present values transform; absent values are left alone.

const a = optionFunctor.map(5, n => n + 1);    // 6
const b = optionFunctor.map(null, n => n + 1); // null

One Abstraction, Many Containers

This is the payoff. arrayFunctor and optionFunctor share the same Functor interface. Code written against Functor<F> works for both without change.

function bumpAll<F extends URIS>(
  F: Functor<F>,
  fa: Kind<F, number>
): Kind<F, number> {
  return F.map(fa, n => n + 1);
}

Calling the Generic Function

bumpAll does not know or care which container it receives. Pass the instance and the data; the result preserves the container type.

const arr = bumpAll(arrayFunctor, [1, 2]); // number[] -> [2, 3]
const opt = bumpAll(optionFunctor, 9);     // number | null -> 10

Functor Laws

A correct functor obeys two laws: mapping the identity function changes nothing, and mapping two functions in sequence equals mapping their composition. Both array and option instances above satisfy these.

// map(fa, x => x) === fa
// map(map(fa, f), g) === map(fa, x => g(f(x)))

Adding More Instances

Because the abstraction is open, new containers plug in by registering a URI and providing an instance. A Tree, a Result, a Task: each becomes mappable by the same generic code.

// Register URI, implement Functor<"Tree">,
// and bumpAll works on trees too - no edits to bumpAll

Why It Matters

Generic functors are the entry point to functional abstractions in TypeScript: applicatives, monads, traversals. Each is an interface over Kind<F, A> with instances per container. You now understand the machinery that makes fp-ts and Effect possible.

interface Monad<F extends URIS> extends Functor<F> {
  of<A>(a: A): Kind<F, A>;
  chain<A, B>(fa: Kind<F, A>, f: (a: A) => Kind<F, B>): Kind<F, B>;
}

Quick Check

Test your understanding of generic functors.

Recap

You wrote a container-generic map.

  • Functor<F> declares map over Kind<F, A>.
  • Array and Option instances implement it for their URIs.
  • Generic code like bumpAll works for any registered container.
  • Functor laws keep instances well-behaved; richer abstractions build on this.

Course 24 next: building type-safe parsers.

Frequently asked questions

Is the “Generic Functors and Mappers” lesson free?

Yes — the full text of “Generic Functors and Mappers” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Generic Functors and Mappers”?

Write map functions generic over any container. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Generic Functors and Mappers” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The HKT Problem in TypeScript
  2. Defining Type Constructors
  3. The Lightweight HKT Pattern
  4. Generic Functors and Mappers
← Back to TypeScript Academy